![](/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.