简体   繁体   English

Azure AD Graph API和WsFederation身份验证

[英]Azure AD Graph API and WsFederation Authentication

I am trying to implement the Azure AD Graph API in an MVC Web App hosted on Azure. 我正在尝试在Azure上托管的MVC Web应用程序中实现Azure AD Graph API。 The Azure AD is set up correctly as I was able to use the Graph API last year in a previous version before it got updated at some point late last year/this year. 我正确地设置了Azure AD,因为我能够在去年/今年下半年的某个时刻对其进行更新之前,在去年的旧版本中使用Graph API。

I am following the instructions here https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet and using the updated code. 我正在按照https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet上的说明进行操作,并使用更新的代码。 The difference between these two projects is I am using WsFed not OpenID, so some parts are different, namely Startup.Auth.cs. 这两个项目之间的区别是我使用的是WsFed,而不是OpenID,因此某些部分有所不同,即Startup.Auth.cs。 Here is the relevant code in this example project (seen here ): 这是此示例项目中的相关代码(请参见此处 ):

Notifications = new OpenIdConnectAuthenticationNotifications()
{                      
    AuthorizationCodeReceived = (context) =>
    {
        var code = context.Code;
        ClientCredential credential = new ClientCredential(clientId, appKey);
        string userObjectID = context.AuthenticationTicket.Identity.FindFirst(
                "http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
        AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));
        AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
        AuthenticationHelper.token = result.AccessToken;
        return Task.FromResult(0);
    }
}

However, since my website it set up that you have to be logged in through WS-Fed to access anything on the site at all, I try to get a token in Startup.Auth.cs. 但是,由于我的网站设置为必须通过WS-Fed登录才能访问该网站上的所有内容,因此我尝试在Startup.Auth.cs中获得一个令牌。 so that I can simply use AcquireTokenSilent later. 这样我以后可以简单地使用AcquireTokenSilent。 I use the project here https://github.com/AzureADSamples/WebApp-WSFederation-DotNet to setup WS-Fed. 我在这里使用该项目https://github.com/AzureADSamples/WebApp-WSFederation-DotNet来设置WS-Fed。

The problem in Startup.Auth.cs is that I don't have access to the AuthorizationCodeReceived option, only SecurityTokenReceived and SecurityTokenValidated. Startup.Auth.cs中的问题是我无权访问AuthorizationCodeReceived选项,只有SecurityTokenReceived和SecurityTokenValidated。 Neither of these give a good option for an access code or anything that I can use to query the Graph API later in my application. 这些都不是访问代码或以后可在我的应用程序中查询Graph API的任何方法的好选择。 How do I do this? 我该怎么做呢? Any guidance would be greatly appreciated. 任何指导将不胜感激。

unfortunately the WS-Federation protocol does not have any concept of client and access token - the only token being traded is the one sent to you for the web login, and there is no authorization code generated. 不幸的是,WS-Federation协议没有客户端和访问令牌的任何概念-唯一被交易的令牌是发送给您的Web登录令牌,并且没有生成授权代码。 If you need to call the Graph API, I strongly recommend switching to OpenId Connect (which does handle the access token acquisition using the logic you reported above). 如果您需要调用Graph API,我强烈建议您切换到OpenId Connect(使用上面报告的逻辑来处理访问令牌的获取)。 If you absolutely cannot switch out from ws-fed, you need to perform OAuth2 flows manually. 如果您绝对不能从ws-fed中退出,则需要手动执行OAuth2流。 In practice, this means taking the code from https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-AppIdentity-DotNet or https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet and plaster it on top of your app. 实际上,这意味着从https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-AppIdentity-DotNethttps://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet中获取代码并粘贴它放在您应用的顶部。 That's not a very clean cut task, which is why I stand by my recommendation to take advantage of the integrated flow offered by OpenId Connect. 那不是一个很明确的任务,这就是为什么我坚持我的建议以利用OpenId Connect提供的集成流程的原因。 HTH V. HTHV。

I managed to get a Microsoft Graph access token, using this method: Do a server-side POST to your application's oauth2/token endpoint https://login.microsoftonline.com/{tenantId}/oauth2/token , with these parameters: 我使用以下方法设法获得了Microsoft Graph访问令牌:使用以下参数对应用程序的oauth2 / token端点https://login.microsoftonline.com/{tenantId}/oauth2/token执行服务器端POST:

grant_type=client_credentials
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=https://graph.microsoft.com

In the above, <clientSecret> is a valid application key generated through the Azure management portal. 在上面, <clientSecret>是通过Azure管理门户生成的有效应用程序密钥。

Method as described here: https://graph.microsoft.io/en-us/docs/authorization/app_only 此处描述的方法: https : //graph.microsoft.io/en-us/docs/authorization/app_only

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM