ASP. NET Identity modifies the default database to increase the custom field

Source: Internet
Author: User
Tags button type actionlink

Visual Studio 2013

Create a new project first

Select MVC to determine

Open the views\shared\_layout.cshtml file and modify it as you want

Change

[HTML]View Plaincopy print?
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title> @ViewBag title- <span style="Color:rgb (255, 0, 0);" > My ASP. </span></title>
  8. @Styles. Render ("~/content/css")
  9. @Scripts. Render ("~/bundles/modernizr")
  10. </head>
  11. <body>
  12. <div class="NavBar navbar-inverse navbar-fixed-top">
  13. <div class="container">
  14. <div class="Navbar-header">
  15.                  <button type= "button"  class= " Navbar-toggle " data-toggle=" collapse " data-target=>   
  16. <span class="Icon-bar"></span>
  17. <span class="Icon-bar"></span>
  18. <span class="Icon-bar"></span>
  19. </button>
  20. @Html. ActionLink ("<span style=" Color:rgb (255, 0, 0); " > Application name </span> "," Index "," Home ", NULL, new {@class = " Navbar-brand "})
  21. </div>
  22. <div class="navbar-collapse collapse">
  23. <ul class="nav navbar-nav">
  24.                      <li>@ Html.ActionLink ("<span style= "Color: rgb (255, 102, 102);" > home </span> ", " Index ", " Home ") </li>&NBSP;&NBSP;
  25.                      <li> @Html. ActionLink ("<span style= " Color: rgb (255, 0, 0); " > about </span> ", " about ", " Home ") </li>&NBSP;&NBSP;
  26. <li> @Html. ActionLink ("<span style=" Color:rgb (255, 0, 0); " > Contacts </span >, "Contact", "Home")</li>
  27. </ul>
  28. @Html. Partial ("_loginpartial")
  29. </div>
  30. </div>
  31. </div>
  32. <div class="container body-content">
  33. @RenderBody ()
  34. <hr />
  35. <footer>
  36. <p@DateTime. Now.year- <span style="Color:rgb (255, 0, 0); > My ASP. </span></P>
  37. </footer>
  38. </div>
  39. @Scripts. Render ("~/bundles/jquery")
  40. @Scripts. Render ("~/bundles/bootstrap")
  41. @RenderSection ("Scripts", Required:false)
  42. </body>
  43. </html>
<! DOCTYPE html>My ASP. NET application </title> @Styles. Render ("~/content/css") @Scripts. Render ("~/bundles/modernizr") Application name "," Index "," Home ", NULL, new {@class =" Navbar-brand "}) </div> <div class=" navbar- Collapse collapse "> <ul class=" Nav navbar-nav "> <li> @Html. ActionLink ("home "," Index "," Home ") </li> <li> @Html. ActionLink (" about "," about "," home ") </li>                <li> @Html. ActionLink (" contacts", "Contact", "Home") </li> </ul> @Html. Partial ("_loginpartial") </div> </div> </div> <div class= "Contai NER body-content "> @RenderBody () My ASP. NET application </p> </footer> </div> @Scripts. Render ("~/bundles/jquery") @ Scripts.render ("~/bundles/bootstrap") @RenderSection ("Scripts", Required:false) </body> 

Replace the relevant content in the views\home\index.cshtml file.

Open the Web. config file, modify the default database connection string

   <add name= "defaultconnection"   connectionstring =    providername= " System.Data.SqlClient " />   

</connectionStrings>

Save Web. config, run the program, click Register, register a new user to activate the database.

Now open the native mssms or vs Server Explorer, you can see the database PS has been built, display data, you can see the registered users.

To build a template with only the user name and password, in practice we may need additional information, such as I will add the phone, the department and so on.

First open the Package management console:

Enter "enable-migrations" in console to complete the migration

Open The Models\identitymodels.cs file and add the following code

Using microsoft.aspnet.identity.entityframework;//Add Reference using System;namespace pingshuo.models{    //You can add Profile data for the user by adding more properties to your ApplicationUser class, please visit HTTP://GO.MICROSOFT.COM/FW link/? linkid=317594 to learn more.    applicationuser : Identityuser    {        //Add custom user Information field public        string Phone {get; set;}        public string Department {get; set;}    }    public class applicationdbcontext:identitydbcontext<ApplicationUser>    {        public Applicationdbcontext ()            : Base ("DefaultConnection")        {        }}    }

Enter Add-migration "Phone" in console

Then enter:update-database

Then enter: Add-migration "department" and Update-database

Now check the database, there are already more of these two fields

Open the AccountViewModels.cs file and add the Red section:

 
Using System.componentmodel.dataannotations;namespace pingshuo.models{public class Externalloginconfirmationviewmodel {[Required] [Display (name = "user name")] public string UserName {g Et Set }} public class Manageuserviewmodel {[Required] [DataType (Datatype.password)] [Display (Nam        E = "Current password")] public string OldPassword {get; set;}        [Required] [Stringlength (errormessage = "{0} must contain at least {2} characters. ", Minimumlength = 6)] [DataType (Datatype.password)] [Display (Name =" New password ")] public string Newpasswor        d {get; set;}        [DataType (Datatype.password)]        [Display (Name = "Confirm New password")] [Compare ("NewPassword", errormessage = "New Password and confirmation password do not match. ")] public string ConfirmPassword {get; Set }} public class Loginviewmodel {[Required] [Display (name = "user name")] public string UserName        {get; set;}        [Required]        [DataType (Datatype.password)] [DiSplay (Name = "password")] public string Password {get; set;}        [Display (Name = "Remember Me?")]    public bool RememberMe {get; set;} } public class Registerviewmodel {[Required] [Display (name = "user name")] public string UserName { Get Set } [Required] [stringlength (errormessage = "{0} must contain at least {2} characters. ", Minimumlength = 6)] [DataType (Datatype.password)] [Display (Name =" password ")] public string Password { Get Set } [DataType (Datatype.password)] [Display (Name = "Confirm password")] [Compare ("Password", errormessage = "Password and Confirm password does not match. ")] public string ConfirmPassword {get; Set      }the field of the//extension class [Required] [Display (Name = "Phone")] public string Phone {get; set;}        [Required]        [Display (Name = "department")] public string Department {get; set;} By the way, write an instance of the Appliationuser class for later use: public ApplicationUser GetUser () {var user = new Applicationuse R () {UserName = this. UserName, Department = this. Department,};return user; }    }}

Forgot to add the using System at the top;

Save, run, effect as follows:

ASP. NET Identity modifies the default database to increase the custom field

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.