Der Versuch, dieses Beispiel rückzuentwickeln app. Nur ich habe meinen eigenen Dienst nicht erstellt, nur versucht, meine Profilinformationen mithilfe der Microsoft Graph API zu erhalten. Der folgende Fehler wird angezeigt:AdalSilentTokenAcquisitionException: Fehler beim automatischen Abrufen des Tokens, da kein Token im Cache gefunden wurde. Call method AcquireToken
AdalSilentTokenAcquisitionException: Fehler beim automatischen Abrufen des Tokens, da kein Token im Cache gefunden wurde. Call-Methode AcquireToken
Ich bin ziemlich neu, aber ich habe alle stackoverflow Probleme im Zusammenhang mit diesem Fehler durchlaufen und nicht in der Lage, es herauszufinden.
Ich benutze Asp.net Core neueste Version. Ich scheitere immer mit dem obigen Fehler auf AcquireTokenSilentAsync. Irgendwelche Tipps oder Ideen wären hilfreich.
Unten ist was ich bisher habe.
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseSession();
//app.UseCookieAuthentication();
// Populate AzureAd Configuration Values
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"];
ClientId = Configuration["Authentication:AzureAd:ClientId"];
ClientSecret = Configuration["Authentication:AzureAd:ClientSecret"];
GraphResourceId = Configuration["Authentication:AzureAd:GraphResourceId"];
GraphEndpointId = Configuration["Authentication:AzureAd:GraphEndpointId"];
// Configure the OWIN pipeline to use cookie auth.
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = ClientId,
ClientSecret = ClientSecret,
Authority = Authority,
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
ResponseType = OpenIdConnectResponseType.CodeIdToken,
GetClaimsFromUserInfoEndpoint = false,
Events = new OpenIdConnectEvents
{
OnRemoteFailure = OnAuthenticationFailed,
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
}
});
OnAuthorizationCodeReceived:
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
// Acquire a Token for the Graph API and cache it using ADAL. In the TodoListController, we'll use the cache to acquire a token to the Todo List API
string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session));
AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, GraphResourceId);
// Notify the OIDC middleware that we already took care of code redemption.
context.HandleCodeRedemption();
}
MyProfileController:
public async Task<IActionResult> Index()
{
AuthenticationResult result = null;
var user = new ADUser();
try
{
string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
ClientCredential credential = new ClientCredential(Startup.ClientId, Startup.ClientSecret);
var tc = authContext.TokenCache.ReadItems();
result = await authContext.AcquireTokenSilentAsync(Startup.GraphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.RequiredDisplayableId));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
String responseString = await response.Content.ReadAsStringAsync();
List<Dictionary<String, String>> responseElements = new List<Dictionary<String, String>>();
}
}
catch (Exception)
{
throw;
}
return View();
}
Vielen Dank für die Rückmeldung an mich. Ich habe es versucht und es funktioniert immer noch nicht. Allerdings habe ich bemerkt, dass, wenn ich einen Breakpoint auf dieser Zeile setzen: result = erwarten authContext.AcquireTokenSilentAsync (Startup.GraphResourceId, Credential, neue UserIdentifier (userObjectID, UserIdentifierType.UniqueId)); und dann warten Sie ein paar Minuten, der Code wird dann ohne den Fehler fortgesetzt. –
Können Sie die Frage mit ADAL-Protokollen und/oder einem Netzwerk-Trace aktualisieren? Temporäre Anweisungen zum Sammeln von Protokollen von ADAL finden Sie hier: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/527. Die wichtigen Netzwerk-Traces wären Sitzungen, die sich auf login.microsoftonline.com befinden – dstrockis