使用 OpenId Connect 针对 Azure Active Directory 获取令牌
Get Token Using OpenId Connect against Azure Active Directory
我在使用 OpenIdConnect 针对 Azure Active Directory 发出授权令牌请求时遇到问题。
我尝试了多种方法,使用 AuthenticationContext.AcquireTokenByAuthorizationCodeAsync 将我的身份验证代码传递给我们的 AD 租户。
我收到的具体错误是“AADSTS70002:验证凭证时出错。AADSTS50011:请求时提供的回复地址‘http://localhost:5000/api/home/index' does not match the reply address 'http://localhost:5000/signin-oidc’”
sting 授权码。"
我不确定的是为什么 AD 认为我的回复 url 是 signin-oidc。我在我的 AuthorizationContext 实例中将回复 url 设置为“http://localhost:5000/api/home/index”;下面的源代码。我读到尾随 / 可能是个问题,但我在回复中没有看到这一点 url。另外,我在代码中的回复 url 与我在 AD 的网站 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)
},
});
当您使用授权代码流请求访问令牌时,您必须提供与用户刚刚登录时相同的重定向 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;
我在使用 OpenIdConnect 针对 Azure Active Directory 发出授权令牌请求时遇到问题。
我尝试了多种方法,使用 AuthenticationContext.AcquireTokenByAuthorizationCodeAsync 将我的身份验证代码传递给我们的 AD 租户。
我收到的具体错误是“AADSTS70002:验证凭证时出错。AADSTS50011:请求时提供的回复地址‘http://localhost:5000/api/home/index' does not match the reply address 'http://localhost:5000/signin-oidc’” sting 授权码。"
我不确定的是为什么 AD 认为我的回复 url 是 signin-oidc。我在我的 AuthorizationContext 实例中将回复 url 设置为“http://localhost:5000/api/home/index”;下面的源代码。我读到尾随 / 可能是个问题,但我在回复中没有看到这一点 url。另外,我在代码中的回复 url 与我在 AD 的网站 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)
},
});
当您使用授权代码流请求访问令牌时,您必须提供与用户刚刚登录时相同的重定向 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;