繁体   English   中英

Azure AD Graph API和WsFederation身份验证

[英]Azure AD Graph API and WsFederation Authentication

我正在尝试在Azure上托管的MVC Web应用程序中实现Azure AD Graph API。 我正确地设置了Azure AD,因为我能够在去年/今年下半年的某个时刻对其进行更新之前,在去年的旧版本中使用Graph API。

我正在按照https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet上的说明进行操作,并使用更新的代码。 这两个项目之间的区别是我使用的是WsFed,而不是OpenID,因此某些部分有所不同,即Startup.Auth.cs。 这是此示例项目中的相关代码(请参见此处 ):

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

但是,由于我的网站设置为必须通过WS-Fed登录才能访问该网站上的所有内容,因此我尝试在Startup.Auth.cs中获得一个令牌。 这样我以后可以简单地使用AcquireTokenSilent。 我在这里使用该项目https://github.com/AzureADSamples/WebApp-WSFederation-DotNet来设置WS-Fed。

Startup.Auth.cs中的问题是我无权访问AuthorizationCodeReceived选项,只有SecurityTokenReceived和SecurityTokenValidated。 这些都不是访问代码或以后可在我的应用程序中查询Graph API的任何方法的好选择。 我该怎么做呢? 任何指导将不胜感激。

不幸的是,WS-Federation协议没有客户端和访问令牌的任何概念-唯一被交易的令牌是发送给您的Web登录令牌,并且没有生成授权代码。 如果您需要调用Graph API,我强烈建议您切换到OpenId Connect(使用上面报告的逻辑来处理访问令牌的获取)。 如果您绝对不能从ws-fed中退出,则需要手动执行OAuth2流。 实际上,这意味着从https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-AppIdentity-DotNethttps://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet中获取代码并粘贴它放在您应用的顶部。 那不是一个很明确的任务,这就是为什么我坚持我的建议以利用OpenId Connect提供的集成流程的原因。 HTHV。

我使用以下方法设法获得了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

在上面, <clientSecret>是通过Azure管理门户生成的有效应用程序密钥。

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

暂无
暂无

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

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