2017-10-31 1 views
0

Ich versuche derzeit, einen Grafikdienst automatisch einzurichten, wenn meine Anwendung gestartet wird. Ich habe folgenden Code:Abrufen von GraphService accesstoken in ASP.NET Core 2.0 AzureAD

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddAuthentication(sharedOptions => 
     { 
      sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; 
     }) 
     .AddAzureAd(options => 
      { 
       Configuration.Bind("AzureAd", options); 
      }) 
     .AddCookie(); 

     services.AddMvc(); 
    } 

innen oder nach dem AddAzureAd würde Ich mag ein Graph registrieren und so konfigurieren, dass MS AAD Graph Api https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api

Aber ich habe keine Ahnung, wie man eine accesstoken verbinden wovon jedes Beispiel spricht. Ich habe das Kästchen auf der Vorlage "Read" von Graph API angekreuzt, also würde ich das automatisch konfigurieren, leider nicht.

Antwort

1

Um die Zugriffstoken in der asp.net Kern mit OpenIdConnect Protokoll zu erwerben, müssen wir unter OnAuthorizationCodeReceived Veranstaltung wie Code verwenden:

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
{ 
    ClientId = ClientId, 
    Authority = Authority, 
    PostLogoutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"], 
    ResponseType = OpenIdConnectResponseType.CodeIdToken, 
    GetClaimsFromUserInfoEndpoint = false, 

    Events = new OpenIdConnectEvents 
    { 
     OnRemoteFailure = OnAuthenticationFailed, 
     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(); 
} 

Weitere Einzelheiten zum acquire im asp.net Kern access_token, können Sie siehe das folgende Codebeispiel:

active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore

+0

dank werde ich versuchen, mit diesem zu gehen foward und sehen, ob es mit der Standard-Implementierung paßt MS-Vorlagen mit gehen, iDK, wenn sie hinter ihrem 'zu verwenden opendidconnect Ad' Methode. –

Verwandte Themen