gpt4 book ai didi

c# - 如何从 ASP.Net OpenID Connect OWIN 组件设置声明?

转载 作者:可可西里 更新时间:2023-11-01 07:55:24 28 4
gpt4 key购买 nike

在身份验证管道期间添加新声明时,我对使用新的 ASP.Net OpenID Connect 框架有疑问,如下面的代码所示。我不确定幕后发生了多少“魔法”。我认为我的大部分问题都围绕着不太了解 OWIN 身份验证中间件而不是 OpenID Connect。

Q1。我应该从 OwinContext.Authentication.User 手动设置 HttpContext.Current.UserThread.CurrentPrincipal 吗?

Q2。我希望能够像以前使用 System.IdentityModel.Claims.Claim 那样将对象类型添加到声明中。新的 System.Security.Claims.Claim 类只接受字符串值?

Q3。我是否需要在 System.Security.Claims.CurrentPrincipal 中为我的 ClaimsPrincipal 使用新的 SessionSecurityToken 包装器以序列化为 cookie - 我正在使用app.UseCookieAuthentication(new CookieAuthenticationOptions()); 但现在确定在维护我在 SecurityTokenValidated 事件期间添加的任何额外声明方面究竟做了什么?

    public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,

Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
// retriever caller data from the incoming principal
var UPN = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Name).Value;
var db = new SOSBIADPEntities();

var user = db.DomainUser.FirstOrDefault(b => (b.EntityName == UPN));

if (user == null)
{
// the caller was not a registered user - throw to block the authentication flow
throw new SecurityTokenValidationException();
}

var applicationUserIdentity = new ClaimsIdentity();
applicationUserIdentity.AddClaim(new Claim(ClaimTypes.Name, UPN, ""));
applicationUserIdentity.AddClaim(new Claim(ClaimTypes.Sid, user.ID.ToString(CultureInfo.InvariantCulture)));


var applications =
db.ApplicationUser
.Where(x => x.ApplicationChild != null && x.DomainUser.ID == user.ID)
.Select(x => x.ApplicationChild).OrderBy(x => x.SortOrder);

applications.ForEach(x =>
applicationUserIdentity.AddClaim(new Claim(ClaimTypes.System, x.ID.ToString(CultureInfo.InvariantCulture))));

context.OwinContext.Authentication.User.AddIdentity(applicationUserIdentity);

var hasOutlook = context.OwinContext.Authentication.User.HasClaim(ClaimTypes.System, "1");

hasOutlook = hasOutlook;

HttpContext.Current.User = context.OwinContext.Authentication.User;
Thread.CurrentPrincipal = context.OwinContext.Authentication.User;

var usr = HttpContext.Current.User;

var c = System.Security.Claims.ClaimsPrincipal.Current.Claims.Count();


return Task.FromResult(0);
},
}
}
);
}

最佳答案

您添加新的 ClaimsIdentity 是否有特定原因?

完成您的目标的最简单方法是检索通过验证传入 token 生成的 ClaimsIdentity,通过 ClaimsIdentity claimsId = context.AuthenticationTicket.Identity; 拥有它后,只需向其添加声明即可。中间件的其余部分将负责在 session cookie 中将其连同其他所有内容序列化,将结果放入当前 ClaimsPrincipal,以及您似乎试图手动执行的所有其他操作。< br/>哈特
五、

关于c# - 如何从 ASP.Net OpenID Connect OWIN 组件设置声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196931/

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