我是身份和MVC的新手,我正在尝试创建一个MVC应用程序作为我的第一个项目之一。
我已经能够关注few tutorials并成功向ApplicationUser添加了其他属性:IdentityUser类
public class ApplicationUser
: IdentityUser<string, ApplicationUserLogin,
ApplicationUserRole, ApplicationUserClaim>
{
[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
}
我的问题是:
我看到电子邮件设置在App_Start.IdentityConfig.cs中需要一个唯一的电子邮件,有没有办法将其设置为需要像Handle这样的独特自定义属性?
var manager = new ApplicationUserManager(new UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
在Views.Shared._LoginPartial.cshtml的部分视图中,它显示了使用User.Identity.GetUserName()登录应用程序的人的用户名/电子邮件是否可以引用我的一个自定义属性而是诸如FirstName或Handle?
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
答案 0 :(得分:2)
此处已涵盖实施自定义UserValidator
:How can customize Asp.net Identity 2 username already taken validation message?
不是为每个显示句柄的页面请求访问数据库,而是在登录期间添加声明。
定义您自己的声明:
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 :(得分:-1)
1)我会从微软的UserValidator
创建你自己的UserValidator
继承并处理你的额外字段。
<强>更新强>
2)你可以制作自己的扩展方法来获得你添加的额外字段。
public static string GetSomething(this IIdentity identity)
{
if (identity.IsAuthenticated)
{
var userStore = new UserStore<ApplicationUser>(new Context());
var manager = new UserManager<ApplicationUser>(userStore);
var currentUser = manager.FindById(identity.GetUserId());
return something = currentUser.something;
}
return null;
}