Ich habe meine Webanwendung wie folgt mithilfe von OpenIDConnectAuthentication eingerichtet. Die OnAuthorizationCodeReceived
Benachrichtigung verwendet Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.8.Azure B2C Active Directory OpenIDConnect- und Autorisierungscodes
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
MetadataAddress = Settings.AADB2CAuth.SignInPolicyMetaAddress, // https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration?p={policy} policy = B2C_1_SignIn
AuthenticationType = Settings.AADB2CAuth.SignInPolicyId, // B2C_1_SignIn
ClientId = Settings.AADB2CAuth.ClientId, // {guid}
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
AuthorizationCodeReceived = OnAuthorizationCodeReceived
},
RedirectUri = Settings.AADB2CAuth.RedirectUri,
Scope = "openid",
ResponseType = "id_token",
});
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
var code = context.Code;
ClientCredential clientCredential = new ClientCredential(Settings.AADB2CAuth.ClientId, Settings.AADB2CAuth.ClientSecret);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(Settings.ClaimTypes.ObjectIdentifier).Value;
string authority = Settings.AADB2CAuth.Authority; // https://login.microsoftonline.com/{tenant}
AuthenticationContext authContext = new AuthenticationContext(authority, new ADAL.ADALTokenCache(userObjectID));
Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(code, redirectUri, clientCredential, Settings.AADGraphApi.GraphResourceId);
}
Das funktioniert gut. Ein Autorisierungscode wird jedoch nicht mit der id_token
zurückgegeben. Wenn diese oder nur code
-code id_token
ändern, die AuthorizationCodeReceived
Benachrichtigung feuert, aber dann bin ich mit dem Fehler
AADSTS70000: Authentication failed: Authorization Code is malformed or invalid
Grundsätzlich erfüllt, was ich versuche zu tun, die B2C AD wie der Strom in Benutzer signiert zuzugreifen. Ist das überhaupt möglich?
ich meine Authentifizierungsoptionen
new OpenIdConnectAuthenticationOptions
{
AuthenticationType = Settings.AADB2CAuth.SignInPolicyId,
Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}", Settings.AADB2CAuth.Tenant, Settings.AADB2CAuth.SignInPolicyId),
ClientId = Settings.AADB2CAuth.ClientId,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
AuthorizationCodeReceived = OnAuthorizationCodeReceived
},
RedirectUri = Settings.AADB2CAuth.RedirectUri,
Scope = "openid",
ResponseType = "code id_token",
});
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
var code = context.Code;
ClientCredential clientCredential = new ClientCredential(Settings.AADB2CAuth.ClientId, Settings.AADB2CAuth.ClientSecret);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(Settings.ClaimTypes.ObjectIdentifier).Value;
string authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}", Settings.AADB2CAuth.Tenant, Settings.AADB2CAuth.SignInPolicyId);
AuthenticationContext authContext = new AuthenticationContext(authority, new ADAL.ADALTokenCache(userObjectID));
Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(code, redirectUri, clientCredential, Settings.AADGraphApi.GraphResourceId);
}
ich mit einer Ausnahme, deren Einzelheiten sind der HTML-Inhalt einer 404-Seite traf ich jetzt
aktualisiert. Betrachten Sie die Anfragen, glaube ich, weilAcquireTokenByAuthorizationCodeAsync
https://login.microsoftonline.com/tfp/oauth2/token
schaut, um den Autorisierungscode zu senden, was ich nicht denke, dass es sollte?
Es kann sich lohnen, unter Hinweis darauf, dass der Autorisierungscode Header ich zurück erhalten, ist die folgende:
{
"kid": "cpimcore_09252015",
"ver": "1.0"
}
Eine schnelle Google-Suche für diese ergibt one result und dies verweist auf die following issue im Android ADAL. Ich bin mir nicht sicher, ob sich das auf mein Problem bezieht.
Ich verwende 'Microsoft.IdentityModel.Clients.ActiveDirectory' den Auth-Code (siehe aktualisierte Frage) auszutauschen. Ich bin mir nicht sicher, ob es möglich ist, es so zu ändern, dass die Richtlinie im Hauptteil der Anfrage gesendet wird. Ich habe es mit der alternativen URL versucht, die du geliefert hast, aber das hat auch nicht funktioniert. Ich mache eindeutig etwas drastisch falsch. – Mardoxx
Diese Bibliothek (ADAL) ist für das gedacht, was Azure AD "v1 apps" nennt.Technisch gesehen werden B2C-Apps als "v2-apps" oder "konvergierte Apps" betrachtet (innerhalb der URL gibt es ein '/ v2.0'). Es gibt eine neue Bibliothek für v2-Anwendungen namens [MSAL] (https://github.com/AzureAD/microsoft-authentication-library-for-dotnet). Hier ist ein [Beispiel mit MSAL] (https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect-v2). –
@Mardoxx können Sie bestätigen, dass dies das Problem war? –