Create Login,Signout and Registration in Asp .Net Mvc Using Entity

We perform the login and registration functionality using the entity framework, we get to know the entire system step by step. This article is for those who are new to asp.net MVC and want to learn how best to login and register users, which is used in real projects.

We will try to create a basic login and registration page where the user can login and register.

Model MVC

A dynamic website is a website whose content depends on the input of the user. Each site always consists of three parts, which are defined as a three-layer architecture. Currently, in modern software development, this three-bus architecture is better identified as the MVC model.

The SVM is the best example of software design. Using this model, any type of software can easily be designed, developed, debugged, managed, maintained, updated, improved and expanded.

This model essentially consists of three parts that define the three levels of a three-layer architecture, namely the client bus as part of the VMC model representation, the central bus as part of the VMC model control, and the server level as the modal part of the VMC model. The three-layer architecture is thus identified by the module, type and controller that are part of the VCM model.

So let’s start by creating a user table to store user data.

Writing space:

CREATE TABLE [dbo].Company_User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](max) NOT NULL,
[E-Mail] [nvarchar](max) NOT NULL,
[address] [nvarchar](max) NULL,
[encrypted password] [nvarchar](max) NULL,
CONSTRINT [PK_TblUsers] PRIMARY KEUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) TO [PRIMARY]
) TO [PRIMARY] TEXT IMAGE_ON [PRIMARY].

Next step in this article:

  1. Open an image studio and create an empty MVC project.
  2. Add a controller to the project.
  3. Add a structure to the project
  4. Add template classes to the project.
  5. Add a display according to the operation of the controller.
  6. Add an action Result of the action.

Add an empty VMC project

Step 1 –Right-click on the Model folder and add the entity structure to the project.

Step 2 –Right click on the Controller folder in the project and add the Controller.

in your project, that is. HomeController.

Step 3 –Right-click on the Model in the Project Class and Model Class to log in and register.

ApplicationVideoModel Class

LoginViewModel
public class [
[required]
[e-mail address]
[display(name = e-mail : )]
public line e-mail { get ; set ; } } }.

Required][Data type (Data type.Password)][Display (Name = Password: )]Password for public lines { get; set; }} Public line password { get; set; }} Public line password { get; set; }}.

RegistrationClassmodel



public registry overview of class
[

Required]
[Display (name = name: )]
Name of the public line [get; define; }.

Required][E-mail address][String length(150)][Display(Name = E-mail: )]public email line { get ; set ; } }[Display(Name = E-mail: )]public email line { get ; set ; } } public email line

Required]
[Display (name = address: )]
Public control address {get; set; }.

Required]
[Data type (data type password)]
[String length (150, minimum length = 6)]
[Display (name = password: )]
Public String password { get; set; }.
}

 

Step 4 –Create an action result for logging in and registering the HomeController

using MvcDemoProject.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security.

namespace MvcDemoProject.Controllers
{
public class HomeController : Control unit
{
// GET : Home
public action index()
{
return View();
}
// GET : UserDetail//Login authentication// This action method is accessible only when the user connects to [Authorize]public ActionResult UserDetail(){return View();}// GET :Login [HttpGet]public ActionResult Login(){Return View();}//Post:Login [HttpPost] [ValidateAntiForgeryToken]public ActionResult Login(LoginViewModel){as(IsAuthenitcatedUser(LoginViewModel).email, LoginViewModel.password).{//MVC and AuthenticationFormsAuthentication.SetAuthCookie(LoginViewModel.Email, false);return RedirectToAction(UserDetail, Home);}not applicable{ModelState.AddModelError(, your reference is incorrect);}. Return View (LoginViewModel);}// GET:Register Return Type [HttpGet] public ActionResult UserRegistration()}// Post:Registration[HttpPost] [ValidateAntiForgeryToken] public ActionResult UserRegistration(RegistrationViewModel RegistrationViewModel){try{as (ModelState.IsValid){als (!IsEmailExist(RegistrationViewModel.Email)){use (DemoDataBaseEntities db = new DemoDataBaseEntities()){Company_User userobj = new Company_User{Email = RegistrationViewModel.Email,Name = RegistrationViewModel.Name,// For encryption you need to use a robust and secure algorithm/// For clarity I only use the encrypted password Base64 = Base64Encode(RegistrationViewModel).Password),Address = RegistrationViewModel.Address};db.Company_Users.Add(userobj);if (db).SaveChanges()>0){/Stel MVC and Login AuthenticationFormsAuthentication in.SetAuthCookie(RegistrationViewModel.Email, false);return RedirectToAction(UserDetail, Home);}else{ModelState.AddModelError(, something went wrong, try again later!);}}}}{ModelState. AddModelError(, the email already exists, try another one!);}

otherwise
{
ModelState.AddModelError(, the model is not valid);
}
}
capture (exception e)
{
ModelState.AddModelError(, e).message);
}
returns View();
}
Public ActionResult SignOut()
{
FormsAuthentication.SignOut();
returns RedirectToAction(Index, Home);
}

private bool IsEmailExist(string email)
{
bool IsEmailExist = incorrect; use
(DemoDataBaseEntities db = new DemoDataBaseEntities())
{
int count = db.company_user.where(a => a.E-Mail == E-Mail).Tel() ;
if (tel > 0)
{
IsEmailExist = where ;
}
}
IsEmailExist return ;
}
private bool IsAuthenitcatedUser(string e-mail, string password)
{
var encryptpassowrd = Base64Encode(password) ;
bool IsValid = false;
using (DemoDataBaseEntities db = new DemoDataBaseEntities())
{
int count = db.Company_Users.Where(a => a.Email == email && a.Encryptedpassword == encryptpassowrd).Count();
if (count > 0)
{
IsValid = true;
}
}
IsValid return;
}
private static line Base64Encode(string PlainPassword)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainTextPassword);
Return System.Convert.ToBase64String(plainTextBytes);
}.

}
}

Step 5 –Create a view for each action result, right-click on each activity method and follow the next step in the figure

Index.cshtml

@{
Viewpocket.Title = Index;
}.

Welcome to theLogin and Registration Portal.

UserDetail.cshtml

@{
ViewBag.title = UserDetail;
}

Login User data

Hello, user @HttpContext.Current.User.Identity.Name

Registration.cshtml

@MvcDemoProject.Models.LoginViewModel
@{
Name ViewBag.Title = Login;
}

Statement

@Use (Html.BeginForm(Login, Home, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = Shape-Horizontal, Role = Shape }).
{
@Html.Anti-Counterfeit Token()

Use a local account to login.

@Html.ValidationSummary(where, , new { @class = text hazard }))).

@Html.LabelFor(m => m.email, new { @class = col-md-2 control label })).

@Html.TextBoxFor(m => m.e-mail, new { @class = form check })
@Html.ValidationMessageFor(m => m.e-mail, new { @class = text hazard }))

@Html.LabelFor(m => m.password, new { @class = col-md-2 control label })).

@Html.PasswordFor(m => m.Password, new { @class = form control })
@Html.ValidationMessageFor(m => m.Password, new { @class = text-dangerous }))

Registration

@Html.ActionLink(Click to register, register)

}

.cshtml user registration

@ Model MvcDemoProject.Models.RegistrationViewModel
@{
ViewBag.Title = Registration;
}.

User registration

@use (Html.BeginForm(UserRegistration, Home, FormMethod.Post, new { @class = form-horizontal, Role = form })).
{
@Html.Anti-Counterfeit Token()

Create a new account.

@Html.ValidationSummary(, new { @class = danger text })).

@Html.LabelFor(m => m.email, new { @class = col-md-2 control label })).

Html. TextBoxFor(m => m.E-Mail, new { @class = form check })).

@Html.LabelFor(m => m.name, new { @class = col-md-2 control label })).

@Html.TextBoxFor(m => m.name, new { @class = form control })).

@Html.LabelFor(m => m.address, new { @class = control label col-md-2 })).

Html. TextBoxFor(m => m.address, new { @class = form control }))).

@Html.LabelFor(m => m.password, new { @class = col-md-2 control label })).

@Html.PasswordFor(m => m.Password, new { @class = form control })).

Login

}

Step 7-Last and final step We need to define the authentication mode as forms in the web.config file and copy the code below on your label in the web.config file.

 

Lower boat source code

Advantages of the ACCM / Why is the ACCM used?

  • Using MVC, you can let the display part and the logical part, i.e. the designer and the developer, work on the project at the same time.
  • In this case, you can combine the corresponding action/logical method and group the view and the specific template. This allows you to complete your project in a short period of time.
  • The code becomes easier to use.
  • You can attach multiple display parts of a single template. In a project based on ECVM it is easy to modify the code.

History of the LCA

It was first presented at Trygve Renskaug in PARK around 1970. At that time, it was introduced in a programming language called Smalltalk, because it was an object-oriented, dynamically typed reflexive programming language. Later, in 1980, Jim Althoff and his colleagues started implementing a new version of Smalltalk, as reported in an article in the Journal of Object Technology (JOT) around 1988.

Subsequently, many architecture models such as Hierarchical Controller View Model (HMVC), Adapter View Model (MVA), Master View Model (MVP), Model View Model (MVVM) are introduced based on the MVC.

The 28th. In March 1996, NeXT Software, Inc. created the WebObjects framework (a Java application server and a server-based web application framework) (first written in Objective-C). After the start of using this frame, the trend towards the appearance of MVA started.

Related Tags:

directory submission sites,submit site to search engines,submit url to search engines,social bookmarking sites list,free article submission sites,top 10 article submission sites,free article submission sites 2020