Asp.net core 访问ASP.NET Core Startup.cs中的当前用户

Asp.net core 访问ASP.NET Core Startup.cs中的当前用户,asp.net-core,authentication,azure-active-directory,Asp.net Core,Authentication,Azure Active Directory,我是使用Azure AD的经过身份验证的用户,我正在尝试使用下面的中间件向经过身份验证的用户添加角色。问题是,我找不到任何东西可以告诉我如何在Startup类中访问当前用户以添加角色 在控制器或更深层的存储库中讨论的所有内容 有人知道我如何在Startup类中访问用户吗 services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>

我是使用Azure AD的经过身份验证的用户,我正在尝试使用下面的中间件向经过身份验证的用户添加角色。问题是,我找不到任何东西可以告诉我如何在Startup类中访问当前用户以添加角色

在控制器或更深层的存储库中讨论的所有内容

有人知道我如何在Startup类中访问用户吗

services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                options.Events = new OpenIdConnectEvents
                {
                    OnTokenValidated = ctx =>
                    {
                        // claimsIdentity we want to add our roles to...
                        ClaimsIdentity claimsIdentity = HttpContext.User.Identity as ClaimsIdentity;

                        // List of claims
                        var appRoles = new System.Collections.Generic.List<Claim>();

                        foreach (Claim claim in ClaimsPrincipal.Current.FindAll("groups"))
                        {
                            // use the OID and get a friendly name to use as the role (if it exists)
                            var groupStringValue = Configuration[$"AcceptedRoles:{claim.Value}"];
                            if (groupStringValue != null)
                            {
                                // build the list
                                appRoles.Add(new Claim(claimsIdentity.RoleClaimType, groupStringValue));
                            }
                        }

                        if (appRoles.Count > 0)
                        {
                            // if anything in the list, add these claims to the current identity
                            claimsIdentity.AddClaims(appRoles);
                        }

                        return Task.CompletedTask;
                    },
                };
            });
services.Configure(OpenIdConnectDefaults.AuthenticationScheme,选项=>
{
options.Events=新的OpenIdConnectEvents
{
OnTokenValidated=ctx=>
{
//我们想将我们的角色添加到。。。
ClaimsIdentity ClaimsIdentity=HttpContext.User.Identity作为ClaimsIdentity;
//索赔清单
var appRoles=new System.Collections.Generic.List();
foreach(ClaimsPrincipal.Current.FindAll(“组”)中的索赔)
{
//使用OID并获得一个友好的名称用作角色(如果存在)
var groupStringValue=Configuration[$”AcceptedRoles:{claim.Value}“];
if(groupStringValue!=null)
{
//建立列表
approvles.Add(新索赔(claimsIdentity.RoleClaimType,groupStringValue));
}
}
如果(近似计数>0)
{
//如果列表中有任何内容,请将这些声明添加到当前标识中
增加索赔(批准);
}
返回Task.CompletedTask;
},
};
});
当前用户(ClaimsPrincipal)可从
TokenValidatedContext.HttpContext.user
获得:

OnTokenValidated = ctx =>
{
    var user = ctx.HttpContext.User;
    ...
}
但是,由于您正在修改已通过Open ID Connect验证的用户,因此应通过
TokenValidatedContext.Principal
而不是
TokenValidatedContext.HttpContext.user
访问
ClaimsPrincipal
。根据您的场景,您可以直接在默认的
ClaimsIdentity
(其中包含Open ID Connect声明)上添加其他声明,或者出于您自己的目的创建单独的
ClaimsIdentity

OnTokenValidated = ctx =>
{
    // This is the ClaimsIdentity created by OpenID Connect, you can add claims to it directly
    ClaimsIdentity claimsIdentity = ctx.Principal.Identities.FirstOrDefault();
    claimsIdentity.AddClaim(new Claim(...));

    // You can also add a new ClaimsIdentity to hold the claims that you'll add
    ctx.Principal.AddIdentity(new ClaimsIdentity(...))
}

您可以在项目中使用此选项:

httpContextAccessor.HttpContext.User.Identity.Name;
你也可以参考下面的链接


不久前,我自己终于弄明白了这一点,但这个答案与我提出的解决方案非常相似,并指向使用“ctx”访问经过身份验证的用户。