首页 文章

Azure Active Directory - 从VS2013中的模板项目调用Microsoft Graph API

提问于
浏览
0

这是我面临的问题的细分:

  • 我从VS2013创建了一个项目,MVC与多租户组织帐户登录,使用AAD进行身份验证

  • 项目模板对我来说很好但我需要更多,我需要调用图API

  • github上调用图形API的sample项目也可以自行运行,但我们需要将相同的概念应用到基于VS2013模板的项目中

  • 当尝试使用类似方式从我们的解决方案调用图形API时,它不起作用

以下是我过去几天一直在经历的痛苦总结 .

VS2013项目模板将此代码用于Account控制器中的SignIn方法:

WsFederationConfiguration config = FederatedAuthentication.FederationConfiguration.WsFederationConfiguration;
        string callbackUrl = Url.Action("Index", "Home", routeValues: null, protocol: Request.Url.Scheme);
        SignInRequestMessage signInRequest = FederatedAuthentication.WSFederationAuthenticationModule.CreateSignInRequest(
            uniqueId: String.Empty,
            returnUrl: callbackUrl,
            rememberMeSet: false);
        signInRequest.SetParameter("wtrealm", IdentityConfig.Realm ?? config.Realm);
        return new RedirectResult(signInRequest.RequestUrl.ToString());

来自github的示例项目使用:

HttpContext.GetOwinContext()
                .Authentication.Challenge(new AuthenticationProperties {RedirectUri = "/"},
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);

然后在启动类上它捕获AuthorizationCodeReceived,如下所示:

app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {

                ClientId = clientId,
                Authority = Authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    //
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                    //

                    AuthorizationCodeReceived = (context) =>
                    {
                        var code = context.Code;

然后将它保存在TokenCache中,当调用图API时,它会启动带有这样的缓存的AuthenticationContext类

AuthenticationContext authContext = new AuthenticationContext(Startup.Authority,
                new NaiveSessionCache(userObjectID));
            ClientCredential credential = new ClientCredential(clientId, appKey);
            result = authContext.AcquireTokenSilent(graphResourceId, credential,
                new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

我试图做的是:

AuthenticationContext authContext = new AuthenticationContext(authority);
                ClientCredential credential = new ClientCredential(clientId, appKey);

result = await authContext.AcquireTokenAsync(graphResourceId,credential);

这会返回一个较短的令牌,但会丢失一些信

如果使用MVC和组织帐户登录在VS2013中创建新项目,则可以轻松复制此问题,然后尝试调用图API .

我需要一种使用VS2013中的模板项目调用图API的方法 .

1 回答

  • 0

    我们就此问题与Microsoft支持部门联系,以下是该解决方案的摘要 . 从VS2013创建的模板使用WSFederation库进行身份验证 . 没有简单的方法可以使用它来调用Graph API . Microsoft在VS2015中对此进行了纠正,其中相同的模板使用OpenID库进行身份验证,然后您可以调用Graph API .

相关问题