[英]protect asp.net web api 2 project with identity server 4
我有一個帶有 .Net framework 4.8 的 asp.net web api 2 項目和一個集中式 Identity Server 4 項目。 我想在我的 web api 2 項目中驗證從 IS4 生成的 jwt/訪問令牌。 我可以理解它是一個重復的問題,但不知何故我無法找到任何合適的幫助,我不確定缺少什么。 我在 web api 項目中使用IdentityServer3.AccessTokenValidation
進行令牌驗證。
啟動文件
using IdentityServer3.AccessTokenValidation;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(WebApplicationApiNew.Startup))]
namespace WebApplicationApiNew
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "https://localhost:44373",
RequiredScopes = new[] { "api1" },
});
}
}
}
使用有效的 JWT 不記名令牌調用此 API 仍會給出 401:
[Authorize]
[HttpPost]
public String GetName1()
{
if (User.Identity.IsAuthenticated)
{
var identity = User.Identity as ClaimsIdentity;
if (identity != null)
{
IEnumerable<Claim> claims = identity.Claims;
}
return "Valid";
}
else
{
return "Invalid";
}
}
錯誤詳情:
2021-07-24 20:41:25.4133|DEBUG|Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware|Authentication failed
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed. Audiences: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. Did not match: validationParameters.ValidAudience: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' or validationParameters.ValidAudiences: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'.
at Microsoft.IdentityModel.Tokens.Validators.ValidateAudience(IEnumerable`1 audiences, SecurityToken securityToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateAudience(IEnumerable`1 audiences, JwtSecurityToken jwtToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateTokenPayload(JwtSecurityToken jwtToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
at Microsoft.Owin.Security.Jwt.JwtFormat.Unprotect(String protectedText)
at Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.<AuthenticateCoreAsync>d__3.MoveNext()
錯誤顯示:
IDX10214: Audience validation failed
如果您的 JWT 令牌包含“aud”聲明,則身份驗證中間件正在嘗試驗證受眾聲明,但您的選項中未指定任何受眾。 您可以嘗試以下方法之一嗎:
在選項中設置受眾:
觀眾 = "[你的觀眾]"
在選項中禁用受眾驗證:
TokenValidationParameters.ValidateAudience = false;
有關更多詳細信息,請參閱身份服務器文檔:
https://docs.identityserver.io/en/latest/topics/apis.html
我找到了解決我的問題的方法。 IS3 要求aud
聲明(url 中帶有/resources
)存在於 jwt/access 令牌中以驗證令牌。 但另一方面,IS4 已默認停止發出aud
聲明。 因此,我們需要顯式設置 IS4 服務器以發出aud
聲明,以支持使用IdentityServer3.AccessTokenValidation
驗證舊版/舊版訪問令牌。
services.AddIdentityServer(options =>
{
...
options.EmitStaticAudienceClaim = true; // <- for older access token validation
})
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.