[英]WebAPI does not respect Jwt token for authorize attribute
我已经实现了此处的所有步骤以及该站点上的其他教程,以使用 AngularJS 和 WebAPI 在我的应用程序中发布和使用 Jwt。 在我的 Startup.cs 中,当AuthorizeAttribute
存在时,我调用以下函数来告诉应用程序消费者 Jwt 令牌:
private void ConfigureOAuthTokenConsumption(IAppBuilder app)
{
string issuer = MyIssuer;
string audienceId = MyAudienceID;
X509Certificate2 cert = GetMyCertificate();
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audienceId },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new X509CertificateSecurityTokenProvider(issuer, cert)
}
});
}
当我运行应用程序时,我可以正常登录,生成 Jwt 令牌就好了,以角度使用它们并将它们添加到 API 请求中,但我仍然得到 404。我必须在 Jwt 配置中丢失一些东西,所以它是没有得到验证,但我没有收到任何错误。
只是为了它,我尝试实现一个客户AuthorizeAttribute
,并在IsAuthorized
方法中放置一个断点。 可以看到请求中包含了 Bearer 令牌,但是IsAuthorized
返回了 false,并且没有设置 Principal。
有谁知道我可能会错过什么? 我离这里很近。
我可能没有足够的信息来正确回答这个问题,但最终我没有以正确的顺序提交我的 OWIN 配置语句。 在 Startup.cs 我把app.UseWebApi(config);
在配置我的授权模块的代码之前。 把app.UseWebApi(config);
在配置代码的末尾使它开始工作。
我在 ASP.NET 论坛上更好地提出了这个问题: http : //forums.asp.net/p/2070482/5976299.aspx?p=True&t=635803483826595456
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.