visual studio 2013
先新建一个项目
选择MVC,确定
打开 ViewsShared\_Layout.cshtml文件,按自己的要求修改
改
- <!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);">我的 ASP.NET 应用程序</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=".navbar-collapse">
- <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);">应用程序名称</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);">主页</span>", "Index", "Home")</li>
- <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">关于</span>", "About", "Home")</li>
- <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">联系方式</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);">我的 ASP.NET 应用程序</span></p>
- </footer>
- </div>
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/bootstrap")
- @RenderSection("scripts", required: false)
- </body>
- </html>
<!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 - 我的 ASP.NET 应用程序</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=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("应用程序名称", "Index", "Home", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("主页", "Index", "Home")</li>
<li>@Html.ActionLink("关于", "About", "Home")</li>
<li>@Html.ActionLink("联系方式", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
在ViewsHomeIndex.cshtml文件里替换相关内容。
打开web.config文件,修改默认数据库连接字符串
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|aspnet-PingShuo-20131123102758.mdf;Initial Catalog=aspnet-PingShuo-20131123102758;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
保存web.config,运行程序,点击注册,注册一个新用户,以激活数据库。
现在打开本机的MSSMS或VS的服务器资源管理器,可以看到已经建好的数据库PS,显示数据,可以看到已经注册的用户。
以建好的模板只有用户名和密码,实际使用中我们可能还需要其他信息,比如我将添加电话、所在部门等。
首先打开程序包管理控制台:
在控制台中输入“Enable-Migrations“,完成了迁移
打开ModelsIdentityModels.cs文件,增加以下代码
using Microsoft.AspNet.Identity.EntityFramework; //添加引用 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/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { //添加自定义的用户信息字段 public string 电话 { get; set; } public string 部门 { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } } }
在控制台中输入Add-Migration "电话"
接着输入:Update-Database
接着输入:Add-Migration "部门"和Update-Database
现在检查数据库,已经多了这两个字段
打开AccountViewModels.cs文件,增加红色部分:
using System.ComponentModel.DataAnnotations;
namespace PingShuo.Models
{
public class ExternalLoginConfirmationViewModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; }
}
public class ManageUserViewModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "当前密码")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "新密码")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认新密码")]
[Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
}
public class LoginViewModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[Display(Name = "记住我?")]
public bool RememberMe { get; set; }
}
public class RegisterViewModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
//扩展类的字段
[Required]
[Display(Name = "电话")]
public string 电话 { get; set; }
[Required]
[Display(Name = "部门")]
public string 部门 { get; set; }
//顺便编写一个AppliationUser类的实例,以便后用:
public ApplicationUser GetUser()
{
var user = new ApplicationUser()
{
UserName = this.UserName,
部门 = this.部门,
};
return user;
}
}
}
忘了在顶部增加using System;
保存,运行,效果如下: