[英]Azure Active Directory login returns the signature key was not found
我已經使用 Z3A580F142203677F13F0A14FZ 為我的 ASP.NET Core 3.0 Web API 實現了身份驗證。 當我使用[Authorize]
屬性時,我收到 http 401 錯誤響應和消息
Bearer error="invalid_token", error_description="未找到簽名密鑰"
我當前在Startup.cs
中的ConfigureService()
如下所示:
options.AddSecurityDefinition("oauth", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
TokenUrl = new Uri($"https://login.microsoftonline.com/<mytenantid>/oauth2/v2.0/token"),
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/TenantId/oauth2/v2.0/authorize", UriKind.RelativeOrAbsolute),
Scopes = { { "api://<myappid>/user_impersonation", "user_impersonation" } }
}
}
});
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
o.Authority = $"https://login.microsoftonline.com/<mytenantid>/v2.0";
o.Audience = "<myappid>";
o.TokenValidationParameters.ValidAudiences = new string[] { "<myappid>", $"api://<myappid>" };
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
如果我遺漏了什么,請告訴我
我以前回答過很多類似的問題。 根據您的問題,您公開了受 Azure 保護的 web api。 接下來,您需要創建一個客戶端應用程序,並使用該客戶端應用程序調用 api 應用程序。
通常 401 錯誤意味着您的令牌的受眾與您的 api 不匹配。 使用token調用api時,會收到401未授權錯誤。 訪問令牌是根據受眾頒發的,因此您必須確保在請求令牌時將scope
設置為您的 api。 當然你也可以解析令牌,檢查aud
聲明,並確保它是你要調用的 api。
我使用auth 代碼流為你做一個簡單的演示:
接下來,在“API 權限”下,讓您的前端應用程序訪問您的后端 api 應用程序:
解析令牌:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.