Azure AD Graph API和WsFederation身份验证

时间:2015-06-18 02:52:32

标签: c# asp.net-mvc authentication azure owin

我正在尝试在Azure上托管的MVC Web App中实现Azure AD Graph API。 Azure AD设置正确,因为我去年在今年年底/今年的某个时候更新了之前的版本,之前我可以使用Graph API。

我按照https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet中的说明并使用更新的代码。这两个项目的区别在于我使用的是WsFed而不是OpenID,因此有些部分不同,即Startup.Auth.cs。以下是此示例项目中的相关代码(见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);
    }
}

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

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

2 个答案:

答案 0 :(得分:0)

遗憾的是,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提供的集成流程。 HTH 诉

答案 1 :(得分:0)

我设法使用此方法获取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

相关问题