【发布时间】: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