ASP.NET MVC 5 Windows Identity Foundation – Customized User Profile – AccountView Model + Identity Database Struture
\Models\ApplicationUser.cs |
using Microsoft.AspNet.Identity.EntityFramework; namespace CRMPortal.Models { public class ApplicationUser : IdentityUser { public string UserFullName { get; set; } public string UserMail { get; set; } } public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } } } |
\Models\AccountViewModels.cs |
using System.ComponentModel.DataAnnotations; namespace CRMPortal.Models { … … public class RegisterViewModel { [Required] [Display(Name = "User Full Name")] public string UserFullName { get; set; } [Required] [Display(Name = "User Mail")] [EmailAddress] public string UserMail { get; set; } [Required] [Display(Name = "User Name")] [StringLength(10, ErrorMessage = "The {0} must be at least {2} – {1} characters long.", MinimumLength = 4)] public string UserName { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } } |
\Views\Account\Register.cshtml |
@model CRMPortal.Models.RegisterViewModel … … @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <h4>Create a new account.</h4> <hr /> @Html.ValidationSummary() <div class="form-group"> @Html.LabelFor(m => m.UserFullName, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.UserFullName, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.UserMail, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.UserMail, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" class="btn btn-default" value="Register" /> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } |
\Controllers\AccountController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin.Security; using CRMPortal.Models; … … [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.UserName, UserFullName = model.UserFullName, UserMail = model.UserMail }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToAction(“Index", “Home"); } else { AddErrors(result); } } return View(model); } … … |
更改 web.conf 下列部分 |
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v110; … … Initial Catalog=aspnet-DbMigrationExample;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> |
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=VMSER001;Initial Catalog=MVCIdentity;Persist Security Info=True;User ID=sa;Password=P@ssw0rd" providerName="System.Data.SqlClient" /> </connectionStrings> |