希望有人能指出我正确的方向,我需要在我的 api 中验证身份服务器 4 颁发的访问 token 。
已在 API 中设置授权属性。
访问 token 已从服务器正确检索,但在将访问 token 传递给请求时,出现 401 未授权错误,并且未处理任何请求,请求被拒绝。我正在使用 IdentityServer3.AccessTokenValidation nuget 包。
我注意到您可以在 AccessTokenValidation 的 v4 中设置 RequireHttpsMetadata = false,但我不知道在 v3 中如何设置。
这是执行此操作的最佳方法还是我应该寻找另一个方向?
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
});
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string,
string>
();
app.UseIdentityServerBearerTokenAuthentication
(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:5000",
RequiredScopes = new[] { "api2" },
});
}
谢谢
最佳答案
您需要删除 UseCookieAuthentication
并只使用 UseIdentityServerBearerTokenAuthentication
。
使用部分
using System.IdentityModel.Tokens.Jwt;
using IdentityServer3.AccessTokenValidation;
using Microsoft.Owin;
using Owin;
using System.Net;
以下配置适用于我使用 .Net 4.7 和 dentityServer3.Contrib.AccessTokenValidation nuget包
public void ConfigureAuth(IAppBuilder app)
{
JwtSecurityTokenHandler.DefaultInboundClaimFilter.Clear();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:5000",
RequireHttps = false, // For development
RequiredScopes = new List<string> { "openid", "profile", "address", "roles", "offline_access" },
});
}
使用的 nuget 包
Install-Package IdentityModel -Version 3.10.10
Install-Package IdentityServer3.Contrib.AccessTokenValidation -Version 4.0.36
Install-Package Microsoft.IdentityModel.Tokens -Version 5.3.0
Install-Package System.IdentityModel.Tokens.Jwt -Version 5.3.0
Install-Package Microsoft.IdentityModel.Protocols.OpenIdConnect -Version 5.3.0
Install-Package Microsoft.IdentityModel.Protocols -Version 5.3.0
Install-Package Microsoft.IdentityModel.Logging -Version 5.3.0
Install-Package Microsoft.IdentityModel.JsonWebTokens -Version 5.3.0
关于oauth-2.0 - 使用 Identity Server 4 在 Web API 2 Framework 4.x 中进行访问 token 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352564/