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?
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title> @ViewBag title- <span style="Color:rgb (255, 0, 0);" > My ASP. </span></title>
- @Styles. Render ("~/content/css")
- @Scripts. Render ("~/bundles/modernizr")
- </head>
- <body>
- <div class="NavBar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="Navbar-header">
- <button type= "button" class= " Navbar-toggle " data-toggle=" collapse " data-target=>
- <span class="Icon-bar"></span>
- <span class="Icon-bar"></span>
- <span class="Icon-bar"></span>
- </button>
- @Html. ActionLink ("<span style=" Color:rgb (255, 0, 0); " > Application name </span> "," Index "," Home ", NULL, new {@class = " Navbar-brand "})
- </div>
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li>@ Html.ActionLink ("<span style= "Color: rgb (255, 102, 102);" > home </span> ", " Index ", " Home ") </li>&NBSP;&NBSP;
- <li> @Html. ActionLink ("<span style= " Color: rgb (255, 0, 0); " > about </span> ", " about ", " Home ") </li>&NBSP;&NBSP;
- <li> @Html. ActionLink ("<span style=" Color:rgb (255, 0, 0); " > Contacts </span >, "Contact", "Home")</li>
- </ul>
- @Html. Partial ("_loginpartial")
- </div>
- </div>
- </div>
- <div class="container body-content">
- @RenderBody ()
- <hr />
- <footer>
- <p@DateTime. Now.year- <span style="Color:rgb (255, 0, 0); > My ASP. </span></P>
- </footer>
- </div>
- @Scripts. Render ("~/bundles/jquery")
- @Scripts. Render ("~/bundles/bootstrap")
- @RenderSection ("Scripts", Required:false)
- </body>
- </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