C# 缓存具有外部身份验证的用户配置文件.NET核心标识服务器4

C# 缓存具有外部身份验证的用户配置文件.NET核心标识服务器4,c#,asp.net-core,asp.net-core-mvc,asp.net-identity-3,identityserver4,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Identity 3,Identityserver4,我对整个身份概念都很陌生,但我在谷歌上搜索了几次,都没有找到一个我觉得合适的答案 我将.NETCore1.0.0与EFCore和IdentityServer4(ID4)一起使用。 ID4在一个单独的服务器上,我在客户端得到的唯一信息是声明。我希望能够访问完整的(扩展的)用户配置文件,最好是从user.Identity 那么,如何设置User.Identity以填充ApplicationUser模型上的所有属性,而无需每次发送DB请求?我希望在验证时将信息存储在缓存中,直到会话结束 我不想做的是在

我对整个身份概念都很陌生,但我在谷歌上搜索了几次,都没有找到一个我觉得合适的答案

我将.NETCore1.0.0与EFCore和IdentityServer4(ID4)一起使用。 ID4在一个单独的服务器上,我在客户端得到的唯一信息是声明。我希望能够访问完整的(扩展的)用户配置文件,最好是从user.Identity

那么,如何设置User.Identity以填充ApplicationUser模型上的所有属性,而无需每次发送DB请求?我希望在验证时将信息存储在缓存中,直到会话结束

我不想做的是在每个控制器中设置一个查询来获取附加信息。客户端上的所有控制器都将继承自一个基本控制器,这意味着如果有必要,我可以DI一些服务

提前谢谢

客户端

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = "Cookies"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            AuthenticationScheme = "oidc",
            SignInScheme = "Cookies",

            Authority = Configuration.GetSection("IdentityServer").GetValue<string>("Authority"),
            RequireHttpsMetadata = false,
            ClientId = "RateAdminApp"
        });

如果要在identity server上转换声明,对于您的情况(您使用aspnet identity),覆盖
UserClaimsPrincipalFactory
是一种解决方案(请参阅)

或者使用cookie身份验证的
OnSigningIn
事件

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
     AuthenticationScheme = "Cookies",
     Events = new CookieAuthenticationEvents()
     {
         OnSigningIn = async (context) =>
         {
             ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity;
             // get claims from user profile
             // add these claims into identity
         }
     }
});

有关客户端应用程序上的解决方案,请参阅类似问题:

如果您想在identity server上转换声明,对于您的情况(您使用的是aspnet identity),覆盖
UserClaimsPrincipalFactory
是一种解决方案(请参阅)

或者使用cookie身份验证的
OnSigningIn
事件

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
     AuthenticationScheme = "Cookies",
     Events = new CookieAuthenticationEvents()
     {
         OnSigningIn = async (context) =>
         {
             ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity;
             // get claims from user profile
             // add these claims into identity
         }
     }
});

有关客户端应用程序的解决方案,请参见类似问题:

因此,我希望用户配置文件具有的任何其他属性都需要作为声明、预定义或自定义添加?例如,在ApplicationUser模型上使用FirstName并不能真正提供任何价值?是否要在identity server中添加声明?看我的最新消息。看起来我的目标越来越接近了!我唯一关心的是复制属性,我需要确保用户的声明和AspNetUsers属性都需要同步。但是我是否可以使用OnSigningIn将所有属性选择到context.Principal.Identity中,这样可以避免声明和AspNetUsers属性不同步@adem caglinSo我希望用户配置文件具有的任何其他属性都需要作为声明、预定义或自定义添加?例如,在ApplicationUser模型上使用FirstName并不能真正提供任何价值?是否要在identity server中添加声明?看我的最新消息。看起来我的目标越来越接近了!我唯一关心的是复制属性,我需要确保用户的声明和AspNetUsers属性都需要同步。但是我是否可以使用OnSigningIn将所有属性选择到context.Principal.Identity中,这样可以避免声明和AspNetUsers属性不同步@阿德姆·卡格林
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
    public AppClaimsPrincipalFactory(
        UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager,
        IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
    {
    }

    public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
    {
        var principal = await base.CreateAsync(user);

        ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
             new Claim("FirstName", user.FirstName)
        });

        return principal;
    }
}

// register it
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
    {
        AuthenticationScheme = "oidc",
        SignInScheme = "Cookies",

        Authority = Configuration.GetSection("IdentityServer").GetValue<string>("Authority"),
        RequireHttpsMetadata = false,
        ClientId = "RateAdminApp",
        Events = new OpenIdConnectEvents
        {
           OnTicketReceived = e =>
           {
               // get claims from user profile
               // add claims into e.Ticket.Principal
               e.Ticket = new AuthenticationTicket(e.Ticket.Principal, e.Ticket.Properties, e.Ticket.AuthenticationScheme);

               return Task.CompletedTask;
            }
        }
    });
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
     AuthenticationScheme = "Cookies",
     Events = new CookieAuthenticationEvents()
     {
         OnSigningIn = async (context) =>
         {
             ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity;
             // get claims from user profile
             // add these claims into identity
         }
     }
});