我有一个在身份服务器4(v3.1.2)上开发的身份服务器和在.NET Framework 4.6
上开发的.NET Web API。在Web API中,我正在使用Identity Server 3访问令牌验证库(v2.14.0)验证传入请求的令牌。
[当我尝试使用由身份服务器生成的JWT令牌访问.NET Web API上的资源时,我总是会收到未经授权的401响应。我已经在.NET Web API中如下设置了Owin中间件。
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:9080/IdentityServer"
});
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
}
但是,为了找出这是Identity Server 4令牌和Identity Server 3访问令牌验证库之间的问题,我使用Identity Server 3库(v2.6.3)创建了一个单独的Identity Server,并提供了生成的令牌从它到我以前使用的相同Web API(与上述Startup.cs相同)。
此请求已成功授权,所有请求均按预期工作。
我的问题是:
是否可以使用来自身份服务器4的令牌来使用身份服务器3访问令牌验证库进行验证?还是我做错了什么?
我不确定,但我认为您的授权URL不正确。我有一个与您类似的情况,我使用IdentityServer3.AccessTokenValidation NuGet包解决了该问题,它工作得很好。因此,我确定您的问题与中间件无关。
尝试替换startup.cs
文件中的以下代码,然后一切正常。
public class Startup
{
public void Configuration(IAppBuilder app)
{
IdentityServerBearerTokenAuthenticationOptions options = new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:9080",
AuthenticationType = "Bearer"
};
app.UseIdentityServerBearerTokenAuthentication(options);
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
}
我希望这能解决您的问题!