【问题标题】:Azure Active Directory - Calling Microsoft Graph API from the template project in VS2013Azure Active Directory - 从 VS2013 中的模板项目调用 Microsoft Graph API
【发布时间】:2016-08-30 23:55:48
【问题描述】:

以下是我面临的问题的细分:

  • 我从 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中,当调用graph 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);

结果 = 等待 authContext.AcquireTokenAsync(graphResourceId, credential);

这会返回一个较短的令牌,其中包含一些缺失的信息。

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

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

【问题讨论】:

  • 您需要从 AD 获得什么样的信息?你需要使用这个token来调用graph API
  • 使用 AAD 进行身份验证的简单方法仅返回用户的登录名。我们的一位客户的登录信息与他们的电子邮件不同。我需要得到他们的电子邮件。具有此信息的图形返回一个名为“mail”的参数。

标签: azure openid ws-federation azure-ad-graph-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 2020-04-27
    • 2022-01-09
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多