Asp.net Identity 修改默认数据库,增加自定义字段

visual studio 2013    

先新建一个项目

选择MVC,确定

打开 ViewsShared\_Layout.cshtml文件,按自己的要求修改

  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);">我的 ASP.NET 应用程序</span></title>  
  8.     @Styles.Render("~/Content/css")  
  9.     @Scripts.Render("~/bundles/modernizr")  
  10.   
  11. </head>  
  12. <body>  
  13.     <div class="navbar navbar-inverse navbar-fixed-top">  
  14.         <div class="container">  
  15.             <div class="navbar-header">  
  16.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  17.                     <span class="icon-bar"></span>  
  18.                     <span class="icon-bar"></span>  
  19.                     <span class="icon-bar"></span>  
  20.                 </button>  
  21.                 @Html.ActionLink("<span style="color: rgb(255, 0, 0);">应用程序名称</span>", "Index", "Home", null, new { @class = "navbar-brand" })  
  22.             </div>  
  23.             <div class="navbar-collapse collapse">  
  24.                 <ul class="nav navbar-nav">  
  25.                     <li>@Html.ActionLink("<span style="color: rgb(255, 102, 102);">主页</span>", "Index", "Home")</li>  
  26.                     <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">关于</span>", "About", "Home")</li>  
  27.                     <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">联系方式</span>", "Contact", "Home")</li>  
  28.                 </ul>  
  29.                 @Html.Partial("_LoginPartial")  
  30.             </div>  
  31.         </div>  
  32.     </div>  
  33.     <div class="container body-content">  
  34.         @RenderBody()  
  35.         <hr />  
  36.         <footer>  
  37.             <p>© @DateTime.Now.Year - <span style="color: rgb(255, 0, 0);">我的 ASP.NET 应用程序</span></p>  
  38.         </footer>  
  39.     </div>  
  40.   
  41.     @Scripts.Render("~/bundles/jquery")  
  42.     @Scripts.Render("~/bundles/bootstrap")  
  43.     @RenderSection("scripts", required: false)  
  44. </body>  
  45. </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;

保存,运行,效果如下:

原文地址:https://www.cnblogs.com/zxh1919/p/7865888.html