J'utilise Azure Ad et j'ai configuré mon fichier Startup.Auth.cs
comme suit Je peux me connecter et utiliser Azure, Google, MS et Linked in pour m'authentifier et j'ai reçu un id_token
de retour, mais je veux être en mesure de valider ce jeton que je reçois d'Azure, mais je ne suis pas sûr de savoir comment. L'événement SecurityTokenValidated
déclenché signifie-t-il que le jeton a déjà été validé par rapport au TokenValidationParameters
que j'ai défini et que je n'ai pas besoin de valider le jeton? si c'est le cas, que dois-je mettre dans le TokenValidationParameters
?le rappel SecurityTokenValidated valide automatiquement le jeton en utilisant Owin Middleware
Le id_token que je reçois ne arrière contient pas une signature cryptée pour valider
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
SlidingExpiration = true,
LoginPath = new PathString("/"),
CookieSecure = CookieSecureOption.Always,
});
var options = new OpenIdConnectAuthenticationOptions
{
Authority = "https://login.windows.net/common",
ClientId = clientId,
RedirectUri = redirectUri,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = AuthenticationFailed,
RedirectToIdentityProvider = OnRedirectToIdentityProvider,
SecurityTokenReceived = OnSecurityTokenReceived,
AuthorizationCodeReceived = OnAuthorizationCodeReceived,
SecurityTokenValidated = OnSecurityTokenValidated,
MessageReceived = OnMessageReceived
},
Scope = "openid",
ResponseType = "id_token",
Description = new AuthenticationDescription
{
AuthenticationType = "OpenIdConnect",
},
ConfigurationManager = new PolicyConfigurationManager(
string.Format(CultureInfo.InvariantCulture, aadInstance, tenant, "/v2.0", OidcMetadataSuffix),
new[] { SisuGoogle, SisuLinkedIn, SisuMicrosoft, SisuLocal, ResetPasswordLocalPolicyId }),
TokenValidationParameters = new TokenValidationParameters
{
ValidAudiences = new string[]
{
"http://localhost:44330/",
},
IssuerSigningKey = GetSecurityKey(),
// If you don't add this, you get IDX10205
//ValidateIssuer = false,
},
};
app.UseOpenIdConnectAuthentication(options);
private SecurityKey GetSecurityKey()
{
var securityKey = "secure key";
var signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha256Signature,SecurityAlgorithms.Sha256Digest);
return signingCredentials.SigningKey;
}
private Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> arg)
{
//do I need to validate the token here or has it already been validated??
//if I have to validate it then how do I? I've tried the following but does not work
var tokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = GetSecurityKey()
};
SecurityToken validatedToken;
var jwtHandler = new JwtSecurityTokenHandler();
//crashes at this point
jwtHandler.ValidateToken(arg.ProtocolMessage.IdToken, tokenValidationParameters, out validatedToken);
return Task.FromResult(0);
}
J'ai la même question. Qu'avez-vous fini par faire? – Mukus
@Mukus J'ai fini par construire ma propre validation en utilisant la cryptographie. J'ai depuis déménagé de ce projet. Cependant, je crois que le callback n'est levé que lors de la validation réussie du jeton, ce qui signifie que oui, le jeton a été validé au moment où ce callback est levé. Cependant, à ce moment-là, je n'ai pas pu trouver de documentation à l'appui. La documentation a peut-être changé maintenant – kurasa