Asp.net mvc 添加自定义用户声明的正确位置

Asp.net mvc 添加自定义用户声明的正确位置,asp.net-mvc,asp.net-identity,Asp.net Mvc,Asp.net Identity,有一个用户类,它有一个名为“Avatar”的字段,用于存储他个人资料图片的路径。我想在局部视图的标题中显示它。所以我决定给用户身份添加一个声明。我将这行代码放在我的IdentityConfig.cs类中: public override Task<ClaimsIdentity> CreateUserIdentityAsync(AppUser user) { if(!System.String.IsNullOrEmpty(user.Avatar

有一个用户类,它有一个名为“Avatar”的字段,用于存储他个人资料图片的路径。我想在局部视图的标题中显示它。所以我决定给用户身份添加一个声明。我将这行代码放在我的
IdentityConfig.cs
类中:

 public override Task<ClaimsIdentity> CreateUserIdentityAsync(AppUser user)
        {
            if(!System.String.IsNullOrEmpty(user.Avatar))
                user.Claims.Add(new AppUserClaim() { ClaimType = "avatar", ClaimValue = user.Avatar});

            return user.GenerateUserIdentityAsync((AppUserManager)UserManager);
        }
它调用用户类的
GenerateUserIdentityAsync
方法。在这个时刻,我变得不清楚了。乍一看,有两种生成用户身份的类似方法:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(User user, UserManager<User> manager)
{
    var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie).ConfigureAwait(false);

    userIdentity.AddClaim(new Claim("Key", "Value"));

    return userIdentity;
}
  • AppUser类内部,该类将
    usermanager
    类作为参数-
    public async Task GenerateUserIdentityAsync(AppUserManager管理器)
  • 内部SignInManager-
    公共覆盖任务CreateUserIdentityAsync(AppUser用户)

  • 这样做的目的是什么?在哪里使用了这些方法?我应该使用哪一个来添加自定义用户声明?

    我已经对标准ASP.NET MVC项目进行了一些重构,因此我不重复添加声明的代码

    Startup.Auth.cs:

    public void ConfigureAuth(IAppBuilder app, Container container)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => IdentityHelper.GenerateUserIdentityAsync(user, manager))
            }
        });
    }
    
    public void ConfigureAuth(IAppBuilder应用程序,容器)
    {
    app.UseCookieAuthentication(新的CookieAuthenticationOptions
    {
    Provider=新CookieAuthenticationProvider
    {
    OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
    validateInterval:TimeSpan.FromMinutes(30),
    regenerateIdentity:(管理者,用户)=>IdentityHelper.GenerateUserIdentityAsync(用户,管理者))
    }
    });
    }
    
    然后我创建了一个静态助手方法来生成标识:

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(User user, UserManager<User> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie).ConfigureAwait(false);
    
        userIdentity.AddClaim(new Claim("Key", "Value"));
    
        return userIdentity;
    }
    
    公共异步任务GenerateUserIdentityAsync(用户用户、用户管理器)
    {
    var userIdentity=await manager.createidentitysync(用户,DefaultAuthenticationTypes.applicationcokie).ConfigureAwait(false);
    AddClaim(新声明(“键”、“值”));
    返回用户身份;
    }
    
    现在,您将能够从SignInManager重用此帮助程序

    公共类应用程序SignInManager:SignInManager
    {
    公共应用程序签名管理器(ApplicationUserManager用户管理器、IAAuthenticationManager authenticationManager)
    :base(userManager、authenticationManager)
    {
    }
    公共覆盖任务CreateUserIdentityAsync(用户)
    {
    返回标识Helper.GenerateUserIdentityHelperAsync(用户,(ApplicationUserManager)用户管理器);
    }
    }
    
    我对一个标准的ASP.NET MVC项目进行了一些重构,因此我不会重复添加声明的代码

    Startup.Auth.cs:

    public void ConfigureAuth(IAppBuilder app, Container container)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => IdentityHelper.GenerateUserIdentityAsync(user, manager))
            }
        });
    }
    
    public void ConfigureAuth(IAppBuilder应用程序,容器)
    {
    app.UseCookieAuthentication(新的CookieAuthenticationOptions
    {
    Provider=新CookieAuthenticationProvider
    {
    OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
    validateInterval:TimeSpan.FromMinutes(30),
    regenerateIdentity:(管理者,用户)=>IdentityHelper.GenerateUserIdentityAsync(用户,管理者))
    }
    });
    }
    
    然后我创建了一个静态助手方法来生成标识:

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(User user, UserManager<User> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie).ConfigureAwait(false);
    
        userIdentity.AddClaim(new Claim("Key", "Value"));
    
        return userIdentity;
    }
    
    公共异步任务GenerateUserIdentityAsync(用户用户、用户管理器)
    {
    var userIdentity=await manager.createidentitysync(用户,DefaultAuthenticationTypes.applicationcokie).ConfigureAwait(false);
    AddClaim(新声明(“键”、“值”));
    返回用户身份;
    }
    
    现在,您将能够从SignInManager重用此帮助程序

    公共类应用程序SignInManager:SignInManager
    {
    公共应用程序签名管理器(ApplicationUserManager用户管理器、IAAuthenticationManager authenticationManager)
    :base(userManager、authenticationManager)
    {
    }
    公共覆盖任务CreateUserIdentityAsync(用户)
    {
    返回标识Helper.GenerateUserIdentityHelperAsync(用户,(ApplicationUserManager)用户管理器);
    }
    }