Festlegen der Forderungen aus ASP.Net OpenID Connect OWIN-Komponenten?
Ich habe Fragen bei der Nutzung der neuen ASP.Net OpenID Connect-Rahmen, während das hinzufügen neuer Forderungen während der Authentifizierung pipeline wie im code gezeigt unten. Ich bin mir nicht sicher, wie viel "Magie" geschieht hinter den kulissen. Ich glaube, die meisten meiner Fragen drehen sich um, nicht zu wissen viel über OWIN Authentifizierungs-middleware im Gegensatz zu OpenID Connect.
Q1. Sollte ich die manuelle Einstellung HttpContext.Current.User
und Thread.CurrentPrincipal
aus OwinContext.Authentication.User
?
Q2. Ich möchte die Möglichkeit zum hinzufügen von Objekt-Typen zu Behauptungen wie ich verwendet, um mit System.IdentityModel.Claims.Claim
. Die neue System.Security.Claims.Claim
Klasse akzeptiert nur string-Werte?
Q3. Brauche ich für die Nutzung der neuen SessionSecurityToken
wrapper für meine ClaimsPrincipal
im System.Security.Claims.CurrentPrincipal
zum serialisieren in eine cookie - ich bin mit app.UseCookieAuthentication(new CookieAuthenticationOptions());
aber jetzt sicher, was das macht genau das, im Hinblick auf die Aufrechterhaltung weitergehende Ansprüche, die ich während der SecurityTokenValidated
Veranstaltung?
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);
},
}
}
);
}
Du musst angemeldet sein, um einen Kommentar abzugeben.
Gibt es einen bestimmten Grund, für das Sie eine neue
ClaimsIdentity
?Der einfachste Weg, das zu tun, was Sie anstreben, ist das abrufen der
ClaimsIdentity
erzeugt wurde, indem Sie überprüfen die eingehenden token, überClaimsIdentity claimsId = context.AuthenticationTicket.Identity;
sobald Sie es haben, fügen Sie einfach Ansprüche an Sie. Der rest der middleware kümmern serialisiert in die session-cookie, zusammen mit allem anderen, legen das Ergebnis in der aktuellenClaimsPrincipal
, und all die anderen Dinge, die Sie zu sein scheinen, versuchen, manuell zu tun.HTH
V.