简体   繁体   English

WebAPI 不尊重 Jwt 令牌的授权属性

[英]WebAPI does not respect Jwt token for authorize attribute

I have implemented all the steps here and other tutorials at that site to issue and consume Jwt in my application using AngularJS and WebAPI.我已经实现了此处的所有步骤以及该站点上的其他教程,以使用 AngularJS 和 WebAPI 在我的应用程序中发布和使用 Jwt。 In my Startup.cs I am calling the following function to tell the app to consumer Jwt tokens when AuthorizeAttribute is present:在我的 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)
                }
            });
    }

When I run the application I can login just fine, generate the Jwt tokens just fine, consume them in angular and add them to the API requests, but I still get a 404. I must be missing something in the Jwt configuration so that it is not being validated, but I'm not getting any errors.当我运行应用程序时,我可以正常登录,生成 Jwt 令牌就好了,以角度使用它们并将它们添加到 API 请求中,但我仍然得到 404。我必须在 Jwt 配置中丢失一些东西,所以它是没有得到验证,但我没有收到任何错误。

Just for the heck of it I tried implementing a customer AuthorizeAttribute , and put a breakpoint in the IsAuthorized method.只是为了它,我尝试实现一个客户AuthorizeAttribute ,并在IsAuthorized方法中放置一个断点。 You can see that the request contains the Bearer token, but IsAuthorized returns false, and the Principal is not set.可以看到请求中包含了 Bearer 令牌,但是IsAuthorized返回了 false,并且没有设置 Principal。

Does anyone have an idea what I might be missing?有谁知道我可能会错过什么? I'm so close here.我离这里很近。

I probably didn't have enough info in here to answer this properly, but it ended up being that I didn't submit my OWIN config statements in the right order.我可能没有足够的信息来正确回答这个问题,但最终我没有以正确的顺序提交我的 OWIN 配置语句。 In Startup.cs I put app.UseWebApi(config);在 Startup.cs 我把app.UseWebApi(config); before the code that configured my authorization modules.在配置我的授权模块的代码之前。 Putting app.UseWebApi(config);app.UseWebApi(config); at the end of the configuration code made it start working.在配置代码的末尾使它开始工作。

I posed this question better at the ASP.NET forums: http://forums.asp.net/p/2070482/5976299.aspx?p=True&t=635803483826595456我在 ASP.NET 论坛上更好地提出了这个问题: http : //forums.asp.net/p/2070482/5976299.aspx?p=True&t=635803483826595456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM