How To Customize Identity In ASP.NET MVC 5

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.

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.).

2. Change project name from WebApplication1 to MIC-Session or you can give any other name.
3. After successfully creating project now, open Solution Explorer and click on Model Folder Then click on class name IdentityModels.cs.

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

  1. public string UniversityName { getset; }  
  3. public string AdminName { getset; }  
  5. public string City { getset; }  
  7. public string PostalCode { getset; }  
  9. public string StreetAddress { getset; }  
  11. public string Country { getset; } 
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(),
  1. [Required]  
  2. [DataType(DataType.Text)]  
  3. [Display(Name = "University Name:")]  
  4. public string UniversityName  
  5. {  
  6.     get;  
  7.     set;  
  8. }  
  9. [Required]  
  10. [DataType(DataType.Text)]  
  11. [Display(Name = "Admin Name:")]  
  12. public string AdminName  
  13. {  
  14.     get;  
  15.     set;  
  16. }  
  17. [Required]  
  18. [DataType(DataType.Text)]  
  19. public string City  
  20. {  
  21.     get;  
  22.     set;  
  23. }  
  24. [Required]  
  25. [DataType(DataType.PostalCode)]  
  26. [Display(Name = "Postal Code:")]  
  27. public string PostalCode  
  28. {  
  29.     get;  
  30.     set;  
  31. }  
  32. [Required]  
  33. [DataType(DataType.MultilineText)]  
  34. [Display(Name = "Street Address:")]  
  35. public string StreetAddress  
  36. {  
  37.     get;  
  38.     set;  
  39. }  
  40. [Required]  
  41. [DataType(DataType.Text)]  
  42. [Display(Name = "Country:")]  
  43. public string Country  
  44. {  
  45.     get;  
  46.     set;  

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}; 

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

8. Now build your project and run it (Press Ctrl+ F5). After this you can see your new entry of form here,
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,
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.

12.  Now you will Successfully login into you site.

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.
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!

Similar Articles