gpt4 book ai didi

c# - 如何使 Asp.Net Core Identity 与 OpenIdConnect 一起工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:27 26 4
gpt4 key购买 nike

如何从 asp.net core 项目重新生成 open id connect 自定义声明?

我已经设置手动映射到声明类型名称,但有时我需要从事件 OnTicketRecieved 之外更新其他声明,即来自 Controller ,因此在那个阶段我确实需要以某种方式重新生成声明。我通过以下方式设置了 openIdConnect:

        _services
.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
options.ClientId = clientId;
options.ClientSecret = clientSecret;
options.Authority = $"{baseAuthorityUrl}/{tenantId}";
options.CallbackPath = new PathString(callBackPath);
options.Scope.Add("email");
options.Scope.Add("profile");

options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
};

options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = e =>
{

return Task.CompletedTask;
},
OnTicketReceived = e =>
{
e.Principal.Identities.First().AddClaim(new Claim(ClaimTypes.Name, e.Principal.FindFirstValue("name")));

return Task.CompletedTask;
}
};
})

我如何从 Controller 重新生成声明?我在想只是以某种方式覆盖 signInManager.RefreshSignInAsync(user)。

最佳答案

如果你想在初始化登录后在 Controller 中添加声明,你应该让身份验证管理器使用新身份:

if (HttpContext.User.Identity is ClaimsIdentity identity)
{

identity.AddClaim(new Claim("userId", "1234"));
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(HttpContext.User.Identity));
}

关于c# - 如何使 Asp.Net Core Identity 与 OpenIdConnect 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55580628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com