【发布时间】:2017-05-15 02:13:33
【问题描述】:
我按照以下官方步骤尝试了“web app calling a Web API in Azure Ad B2C”场景,唯一的区别是我使用的是Asp.Net core。我正在使用 AuthorizationCode 获取访问令牌,但它总是返回 id 令牌和 NULL 访问令牌。
- Create an Azure AD B2C tenant。
- Register a web api。
- Register a web app。
- Set up policies。
- Grant the web app permissions to use the web api。
我的代码:
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme,
AutomaticChallenge = true,
ClientId = aadB2cSettings.ClientId,
MetadataAddress = $"{aadB2cSettings.Instance}{aadB2cSettings.Tenant}/v2.0/.well-known/openid-configuration?p={aadB2cSettings.B2cSignUpOrSignInPolicy}",
PostLogoutRedirectUri = aadB2cSettings.RedirectUrl,
ResponseType = OpenIdConnectResponseType.CodeIdToken,
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},
Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = async context =>
{
var authCode = context.TokenEndpointRequest.Code;
var b2cAuthority = $"{aadB2cSettings.Instance}tfp/{aadB2cSettings.Tenant}/{aadB2cSettings.B2cSignUpOrSignInPolicy}/v2.0/.well-known/openid-configuration";
var cca = new ConfidentialClientApplication(
aadB2cSettings.ClientId,
b2cAuthority,
aadB2cSettings.RedirectUrl,
new ClientCredential(aadB2cSettings.ClientSecret),
new TokenCache(),
null);
try
{
var authResult = await cca.AcquireTokenByAuthorizationCodeAsync(authCode, new[] { "https://hulab2c.onmicrosoft.com/b2cdemo/all" });
context.HandleCodeRedemption(authResult.AccessToken, authResult.IdToken);
}
catch (Exception ex)
{
throw ex;
}
}
},
使用 fiddler 来捕获请求,它是:
发布 https://login.microsoftonline.com/hulab2c.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_signuporsignin HTTP/1.1
请求正文:
client_id=1ff91f47-08ee-4973-83f4-379ad7e0679c&client_info=1&client_secret=......&scope=https%3A%2F%2Fhulab2c.onmicrosoft.com%2Fb2cdemo%2Fall+offline_access+openid+profile&grant_type=authorization_code&code=。 .....&redirect_uri=https%3A%2F%2Flocalhost%3A44383%2F
返回:
{"id_token":"......","token_type":"Bearer","not_before":1494494423,"client_info":"......","scope":"" }
所以只有 id 令牌,没有访问令牌。但是我们应该在这里获取访问令牌,对吧?
【问题讨论】:
标签: azure-ad-b2c