在身份验证管道期间添加新声明时,我对使用新的 ASP.Net OpenID Connect 框架有疑问,如下面的代码所示。我不确定幕后到底发生了多少“魔法”。我认为我的大部分问题都围绕着对 OWIN 身份验证中间件了解不多,而不是 OpenID Connect。
Q1。我应该手动设置HttpContext.Current.User
和Thread.CurrentPrincipal
fromOwinContext.Authentication.User
吗?
Q2。我希望能够像以前那样将对象类型添加到声明中System.IdentityModel.Claims.Claim
。新System.Security.Claims.Claim
类只接受字符串值?
Q3。我是否需要为我的 in使用新的SessionSecurityToken
包装器来序列化为 cookie - 我正在使用 但现在确定在维护我在事件期间添加的任何其他声明方面究竟做了什么?ClaimsPrincipal
System.Security.Claims.CurrentPrincipal
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);
},
}
}
);
}