【问题标题】:WebAPI does not respect Jwt token for authorize attributeWebAPI 不尊重授权属性的 Jwt 令牌
【发布时间】:2016-01-07 17:20:27
【问题描述】:

我已经实现了所有步骤 here 和该站点上的其他教程,以使用 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 token,但是IsAuthorized返回false,并且没有设置Principal。

有人知道我可能缺少什么吗?我离这里很近。

【问题讨论】:

    标签: c# asp.net angularjs asp.net-web-api jwt


    【解决方案1】:

    我可能没有足够的信息来正确回答这个问题,但最终我没有按正确的顺序提交我的 OWIN 配置语句。在 Startup.cs 中,我将 app.UseWebApi(config); 放在配置我的授权模块的代码之前。将app.UseWebApi(config); 放在配置代码的末尾使其开始工作。

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

    【讨论】:

      猜你喜欢
      • 2011-07-18
      • 2019-04-17
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2018-06-05
      • 2020-08-28
      相关资源
      最近更新 更多