关于MVC和Identity的两个问题

本文关键字:两个 问题 MVC Identity 关于 | 更新日期: 2023-09-27 17:51:12

我对标识和MVC非常陌生,我正在尝试创建一个MVC应用程序作为我的第一个项目之一。

我已经能够遵循一些教程,并成功地将其他属性添加到我的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
    }

我的问题是:

  1. 我看到在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
        };
    
  2. 在视图的部分视图.Shared._LoginPartial.chtml中,它显示了使用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>
    }
    

关于MVC和Identity的两个问题

这里已经介绍了实现自定义UserValidator:如何自定义Asp.net Identity 2用户名已接受验证消息?

与其针对每个页面请求都访问数据库以显示句柄,不如在签名期间添加声明。

定义您自己的索赔:

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(我会让您自己的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;
}