![](/img/trans.png)
[英]Azure B2C Bearer error=“invalid_token”, error_description=“The signature key was not found”
[英].Net core api with AD B2C OAuth 2.0 - Invalid_token error
我正在关注此资源。 我可以成功获取令牌,但在第二次调用我的 api 时使用令牌时得到 401。 它说 Bearer error='invalid_token'。 早些时候它给出了“无效的发行者”,所以我解码了令牌以在 appSettings.json 的“实例”字段中使用发行者。 以下是 appSettings 和令牌。 我究竟做错了什么?
应用设置.json
{
"AzureAdB2C": {
"Instance": "https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxxxx/v2.0/",
"ClientId": "452gfsgsdfgsdgssfs5425234",
"Domain": "xxxxxxxxxxxxxxx.onmicrosoft.com",
"SignUpSignInPolicyId": "B2C_1_Auth-SignUpIn"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
令牌
{
"iss": "https://login.microsoftonline.com/23423fsf234234sfsd42342vsx2542/v2.0/",
"exp": 1551878022,
"nbf": 1551874422,
"aud": "ee965664-d1e3-4144-939a-11f77c523b50",
"oid": "a9ee8ebb-433d-424b-ae24-48c73ae9969c",
"sub": "a9ee8ebb-433d-424b-ae24-48c73ae9969c",
"name": "unknown",
"extension_xxxID": "9f27fd88-7faf-e411-80e6-005056851bfe",
"emails": [
"dfgdfgadfgadfg@dgadg.com"
],
"tfp": "B2C_1_Auth-SignUpIn",
"scp": "user_impersonation",
"azp": "4453gdfgdf53535bddhdh",
"ver": "1.0",
"iat": 1551874422
}
AD B2C 实例
Postman - revalapi 突出显示的是上一个镜头中注册应用程序的 uri
令牌
错误
好的。 看起来 AD B2C + .Net Core 对 onmicrosoft.com URI 不满意,即使 Microsoft 文档资源说它确实如此。 见这里。 我必须使用 b2clogin.com uri,如下面的屏幕截图所示。 希望它可以帮助某人。
邮差
应用设置.json
启动.Auth.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
.AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddApplicationInsightsTelemetry();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseMvc();
}
要首先注册您的B2C dotnet 核心应用程序,您必须登录到您的 B2C 租户。
成功注册后,为隐式授权流程配置以下步骤。
回复网址
确保您已相应地完成此步骤:
转到设置并将邮递员回调 URL 添加到: https : //www.getpostman.com/oauth2/callback
正确输入此 URL 后,单击左上角的保存。
请参阅下面的屏幕截图:
编辑清单
对于隐式大流,单击您的应用程序清单并搜索oauth2AllowImplicitFlow属性使其为true
请参阅下面的屏幕截图:
您对 azure B2C 的设置是为隐式授权流API 调用完成的。
邮差
现在启动您的邮递员并选择请求类型为 OAuth 2.0 如下所示:
现在单击获取新访问令牌,将出现新的弹出窗口
请参阅下面的屏幕截图:
在 Auth URL 上添加您的租户 ID ,如下所示:
设置您的客户 ID
设置要访问的范围
现在单击请求令牌作为响应,您将获得隐式授权访问令牌:
看屏幕截图:
使用此令牌访问数据:
在 Token 文本框中复制您已经访问过的令牌,然后选择令牌类型作为不记名令牌
请参阅下面的屏幕截图:
隐式流的棘手部分是将清单属性oauth2AllowImplicitFlow设置为 true
希望这可以解决您的问题。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.