Share via


How To Customize Identity In ASP.NET MVC5

Introduction

ASP.NET Identity is the latest feature in ASP.NET Membership System. The ASP.NET Membership System is used to fulfil the Membership requirement of a site. That involved Forms Authentication, and a SQL Server database for user names, passwords, profile data, etc. But as the web evolve, it is not only about user that it will enter login and password and registered in your application. The current era is really enhanced. A modern membership system must enable redirection-based log-ins to authentication providers like different social network websites.

ASP.NET Identity can be implemented for all types of solutions by customizing it appropriately according to the requirement.

So to customize the ASP.NET Identity according to our requirement, one must have to be some deep understanding to code behind. A simple way to customize the ASP.NET Identity is that we may use the following easy steps to customize the ASP.NET Identity according to our own requirements.

Steps

1. Create an ASP.NET MVC Project with individual account (You may use ASP.NET Identity in other Projects of ASP.NET Web Forms, Web API, SignalR etc.).

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/3.jpg

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/1.png

 

2. Change project name from WebApplication1 to MIC-Session or you can give any other name.

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/2.png

 

3. After successfully creating project now, open Solution Explorer and click on Model Folder Then click on class name IdentityModels.cs.

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/4.jpg

4. Add these properties under the “ApplicationUser:IdentityUser”.

1.public string  UniversityName { get; set; }   
2.public string  AdminName { get; set; }    
3.public string  City { get; set; }    
4.public string  PostalCode { get; set; }    
5.public string  StreetAddress { get; set; }
6.public string  Country { get; set; }

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/5.jpg

 

5. Now go to to AccounViewModel.cs class into the Models folder and then go to the “RegisterView Model()” and modify the code accordingly. Paste this code in the RegisterViewModel(),



      [Required]   
      [DataType(DataType.Text)]   
      [Display(Name =     "University Name:"    )]   
      public string  UniversityName 
      {   
                 get      ;       set      ;       
      }    
   
      [Required]   
      [DataType(DataType.Text)]   
      [Display(Name =     "Admin Name:"    )]   
      public string  AdminName 
      {   
                 get      ;       set      ;       
      }   
   
      [Required]   
      [DataType(DataType.Text)]   
      public string  City 
      {   
                 get      ;       set      ;       
      }    
   
      [Required]   
      [DataType(DataType.PostalCode)]   
      [Display(Name =     "Postal Code:"    )]   
      public string  PostalCode 
      {   
                 get      ;       set      ;      
      }   
   
      [Required]   
      [DataType(DataType.MultilineText)]   
      [Display(Name =     "Street Address:"    )]   
      public string  StreetAddress 
      {  
                 get      ;       set      ;      
      }    
   
      [Required]   
      [DataType(DataType.Text)]   
      [Display(Name =     "Country:"    )]  
      public string  Country 
      {   
                 get      ;       set      ;       
      }  

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/6.jpg

 

6. Now go to AccountController.cs class in Controller Folder and go to “Register(RegisterViewModel model)” and modify accordingly. Add these lines in the Register class.

  1. var user = new ApplicationUser { UserName = model.Email, Email = model.Email,UniversityName=model.UniversityName, AdminName=model.AdminName,City=model.City,PostalCode=model.PostalCode,StreetAddress=model.StreetAddress,Country=model.Country}; 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/8.png

** **

7. Now go to Views Folder from Solution Explorer and go to “Register.cshtml” File and update the code accordingly,

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/10.png

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/11.png

 

8. Now build your project and run it (Press Ctrl+ F5). After this you can see your new entry of form here,

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/12.png

 

9. On filling the form and click on Register you might get error. Don’t worry about it and stop the project. Go back on Project and open it.

  Go to NuGet Package Manager Console,

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/13.png

 

10. Now add the following  commands in Console Manager,

  1. :: Enable-Migrations

  2. :: Add-Migration UpdateTable

  3. :: Update-Database –verbose

11. Now again run the project and fill form and then hit Register Button.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/15.png

 

**12. ** Now you will successfully login into you site.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/16.png

 

13. Now go back to Visual Studio and Open Server Explorer from Visual Studio Side bar and here you can see your Update Database Table.

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/17.png

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/12012015152011PM/18.png

 

14. So that how we’ve customized the ASP.NET Identity in MVC Project. By following these simple steps you may customize the ASP.NET Identity, however you want according to your own requirements!

See Also

Overview of Custom Storage Providers for ASP.NET Identity
Customizing ASP.NET Authentication with Identity