【问题标题】:Get Token Using OpenId Connect against Azure Active Directory使用 OpenId Connect 针对 Azure Active Directory 获取令牌
【发布时间】:2017-06-21 04:02:24
【问题描述】:

我在使用 OpenIdConnect 对 Azure Active Directory 请求授权令牌时遇到问题。

我尝试了多种方法,使用 AuthenticationContext.AcquireTokenByAuthorizationCodeAsync 将我的身份验证代码传递给我们的 AD 租户。

我收到的具体错误是“AADSTS70002:验证凭据时出错。AADSTS50011:回复地址“http://localhost:5000/api/home/index”与请求时提供的回复地址“http://localhost:5000/signin-oidc”不匹配 sting 授权码。”

我不确定为什么 AD 认为我的回复 url 是 signin-oidc。我在我的 AuthorizationContext 实例中将回复 url 设置为“http://localhost:5000/api/home/index”;源代码如下。我已经读过尾随 / 可能是一个问题,但我在回复网址中没有看到这一点。此外,我在代码中的回复 url 与我在 AD 中的 web api 中注册的相同。

任何帮助将不胜感激。我已经阅读了很多关于如何针对 Azure AD 使用 OpenId Connect 的示例,但似乎非常不一致。

 // Configure the OWIN pipeline to use OpenIDConnect.
        app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            //AuthenticationScheme = "oidc",
            Authority = authority,
            ClientId = clientId,
            Scope = { "openid profile email offline" },
            ResponseType = OpenIdConnectResponseType.CodeIdToken,
            TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = false
            },

            Events = new OpenIdConnectEvents
            {
                OnAuthorizationCodeReceived = async context =>
                {
                    var clientCred = new ClientCredential(clientId, clientSecret);

                    var tenantId = "xxxx.onmicrosoft.com";

                    var resource = new Uri(string.Format(organizationHostName, "*"));

                    var authContext = new AuthenticationContext(aadInstance + tenantId);

                    var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code,
                        new Uri(redirectUri), clientCred, "https://login.windows.net/xxxxxxx-xxxx-xxxx-xxxxxxxxxxx/oauth2/token");

                    context.TokenEndpointRequest.RedirectUri = redirectUri;
                }, 

                OnAuthenticationFailed = (context) => Task.FromResult(0)
            },
        });

【问题讨论】:

  • 这是我在调试时注意到的一些附加信息。在 AuthorizationCodeReceivedContext 的属性中,有两个 URI。一个 id 用于重定向,第二个用于 OpenIdConnect.Code.Redirect。我试图了解两者之间的区别。

标签: azure asp.net-web-api active-directory asp.net-core-1.0 openid-connect


【解决方案1】:

当您使用授权代码流请求访问令牌时,您必须提供与用户刚刚登录时相同的重定向 URI。我想应该是 http://localhost:5000

authContext.AcquireTokenByAuthorizationCodeAsync 的最后一个参数应该是您想要访问令牌的 API 的资源 URI。目前,您已将其设置为不起作用的令牌端点 URL。如果您需要 Azure AD Graph API 的令牌,则必须将其设置为 https://graph.windows.net

所以应该是这样的:

var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code,
                    new Uri("http://localhost:5000/signin-oidc"), clientCred, "https://graph.windows.net");

这应该不是必需的:

context.TokenEndpointRequest.RedirectUri = redirectUri;

【讨论】:

  • 非常感谢。周末我也得出了同样的结论。我的功能现在可以工作了。
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 2023-03-23
  • 2018-02-17
  • 2021-08-13
  • 2020-10-30
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
相关资源
最近更新 更多