关于MVC和身份的两个问题

我是身份和MVC的新手,我正在尝试创建一个MVC应用程序作为我的第一个项目之一。

我已经能够遵循一些教程并成功地向我的ApplicationUser添加了其他属性:IdentityUser类

public class ApplicationUser : IdentityUser { [Required] [Display(Name = "UserName")] [StringLength(50)] public string Handle { get; set; } [StringLength(100, ErrorMessage = "Your {0} can be at most {1} characters long.")] [Display(Name = "First Name")] public string FirstName { get; set; } [StringLength(100, ErrorMessage = "Your {0} can be at most {1} characters long.")] [Display(Name = "Last Name")] public string LastName { get; set; } [Required] [Display(Name = "User Creation Date")] public DateTime UserCreationDate { get; set; } public ApplicationUser() { this.Id = Guid.NewGuid().ToString(); // Add any custom User properties/code here } 

我的问题是:

  1. 我看到电子邮件设置为在App_Start.IdentityConfig.cs中需要一个唯一的电子邮件,有没有办法将其设置为需要像Handle这样的独特自定义属性?

      var manager = new ApplicationUserManager(new UserStore(context.Get())); // Configure validation logic for usernames manager.UserValidator = new UserValidator(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; 
  2. 在Views.Shared._LoginPartial.cshtml的部分视图中,它显示了使用User.Identity.GetUserName()登录应用程序的人的UserName / Email,有一种方法可以引用我的一个自定义属性,例如FirstName,还是处理?

     @using Microsoft.AspNet.Identity @if (Request.IsAuthenticated) { using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" })) { @Html.AntiForgeryToken()  } } else {  } 

这里已经介绍了实现自定义UserValidator : 如何自定义Asp.net Identity 2用户名已经采用的validation消息?

而不是为每个显示句柄的页面请求访问数据库,而是在登录期间添加声明。

定义您自己的声明:

 public static class CustomClaimTypes { public const string Handle = "http://schemas.xmlsoap.org/ws/2014/03/mystuff/claims/handle"; } 

在登录期间,设置声明:

 private async Task SignInAsync(ApplicationUser user, bool isPersistent, string password = null) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); //Get the handle and add the claim to the identity. var handle = GetTheHandle(); identity.AddClaim(new Claim(CustomClaimTypes.Handle, handle); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); } 

然后,通过扩展方法,您可以像GetUserName()一样读取它:

 public static class IdentityExtensions { public static string GetHandle(this IIdentity identity) { if (identity == null) return null; return (identity as ClaimsIdentity).FirstOrNull(CustomClaimTypes.Handle); } internal static string FirstOrNull(this ClaimsIdentity identity, string claimType) { var val = identity.FindFirst(claimType); return val == null ? null : val.Value; } } 

最后,在您看来:

 @User.Identity.GetHandle() 

1)我会让你自己的UserValidatorinheritance自microsoft的UserValidator并处理你的额外字段。

更新:

2)您可以创建自己的扩展方法来获取添加的额外字段。

 public static string GetSomething(this IIdentity identity) { if (identity.IsAuthenticated) { var userStore = new UserStore(new Context()); var manager = new UserManager(userStore); var currentUser = manager.FindById(identity.GetUserId()); return something = currentUser.something; } return null; }