1. 程式人生 > >Asp.net Identity 修改默認數據庫,增加自定義字段

Asp.net Identity 修改默認數據庫,增加自定義字段

擴展 studio required ssa 字段 profile 服務器 cat fix

visual studio 2013

先新建一個項目技術分享圖片

選擇MVC,確定技術分享圖片

打開 Views\Shared\_Layout.cshtml文件,按自己的要求修改

[html] view plain copy 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);">我的 ASP.NET 應用程序</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=".navbar-collapse">
  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);">應用程序名稱</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);">主頁</span>", "Index", "Home")</li>
  25. <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">關於</span>", "About", "Home")</li>
  26. <li>@Html.ActionLink("<span style="color: rgb(255, 0, 0);">聯系方式</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);">我的 ASP.NET 應用程序</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>
<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>

Views\Home\Index.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“,完成了遷移

打開Models\IdentityModels.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;

保存,運行,效果如下:

技術分享圖片

Asp.net Identity 修改默認數據庫,增加自定義字段