【发布时间】:2018-11-18 11:50:36
【问题描述】:
我正在使用 Azure AD v1 端点来授权我的 web 应用程序。
在初始身份验证时,我没有让 access_token 成为有效的 jwt 令牌。但是我让 id_token 成为有效的 jwt,而 acces_token 成为 refresh_token 的值,这看起来很奇怪。
我可以使用 id_token 作为不记名令牌来调用我的 Web API。都很好。
现在当 id_token 过期时,我正在使用我的 refresh_token 发送以下刷新令牌请求。我收到未签名的 id_token 作为响应。由于新的 id_token 未签名,因此使用此 id_token 我无法访问 Web API。 我错过了什么吗?
POST /token HTTP/1.1
Host: {authority}
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&
client_id=mvc&
client_secret=secret&
refresh_token=AQABAAAAAADX8GCi6J
&scope=openid%20profile%20offline_access
我正在使用以下启动配置来设置身份验证
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromSeconds(1000);
options.Cookie.Name = "mvcapplication";
})
.AddOpenIdConnect(option=>{
options.Authority = "{aad v1 endpoint}";
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.ResponseMode = "form_post";
options.SignInScheme = "Cookies";
options.CallbackPath = "/Home/Index/";
options.RequireHttpsMetadata = false;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
//Default Scopes
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("offline_access");
});
【问题讨论】:
-
嗯,你应该使用访问令牌来调用 API :)
-
@juunas 在评论 GetClaimsFromUserInfoEndpoint 属性时获得有效的 access_token
-
你刚刚回答了你自己的问题 :) JWT Bearer options Audience 应该是 API 的客户端 ID。如果您将其设置为 Web App 的客户端 id,它将只接受给 Web App 的 id 令牌,并且您本质上共享应用程序身份。
-
当您在 API 的 JWT 选项中配置
Audience/ValidAudience/ValidAudiences时,会配置验证以检查令牌中的aud声明是否与配置的内容匹配。如果没有,你会得到一个 401。 -
@juunas 完美……我现在明白我的错误了……非常感谢!!
标签: azure-active-directory openid-connect refresh-token azure-ad-b2b