【问题标题】:Getting GraphService accesstoken in ASP.NET Core 2.0 AzureAD在 ASP.NET Core 2.0 AzureAD 中获取 GraphService 访问令牌
【发布时间】:2017-10-31 13:19:27
【问题描述】:

我目前正在尝试在我的应用程序启动时自动设置图形服务。我有以下代码:

    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();
    }

在 AddAzureAd 内部或之后,我想注册并配置一个 GraphService 以连接到 MS AAD Graph Api https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api

但我不知道如何获得每个示例都提到的 accesstoken。我从 Graph API 中勾选了模板“读取”上的框,所以我虽然这会自动配置,但遗憾的是它不是。

【问题讨论】:

    标签: asp.net azure asp.net-core azure-active-directory msal


    【解决方案1】:

    要使用 OpenIdConnect 协议获取 asp.net 核心中的访问令牌,我们需要使用OnAuthorizationCodeReceived 事件,如下代码:

    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();
    }
    

    更多关于在asp.net core中获取access_token,可以参考下面的代码示例:

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

    【讨论】:

    • 谢谢,我会继续努力,看看它是否符合 MS 模板附带的标准实现,idk 如果他们在 AddAzureAd 方法后面使用 opendidconnect。
    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2019-12-15
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多