验证asp.net/Authorize中的Node.Js JWT标记

时间:2017-03-20 11:25:32

标签: asp.net node.js jwt

我正在将我的asp.net服务拆分为多个微服务。作为一个过程,我使用Node.Js创建了我的身份服务,它使用JWT作为令牌。

现在我想在C#中使用此令牌,以便我的所有[Authorize]属性都使用此令牌并允许访问。

我已经看了很多实现,但无法让它工作。由于JWT是标准的实施,我不明白为什么这不起作用的原因。

这是我的C#代码

 public void ConfigureAuth(IAppBuilder app)
 {
            var issuer = "myorg/identity2";
            string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
            byte[] audienceSecret = TextEncodings.Base64Url.Decode
            ("xfecrrt7CV");

            // 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 SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret)
                    }
                });

但是,每当我尝试访问受保护的方法时,我都会收到此错误。

{"Message":"Authorization has been denied for this request."}

这里有什么我想念的吗?如何向此添加声明标识?

1 个答案:

答案 0 :(得分:1)

最后,它得到了解决。我的一个朋友调试了Identity源代码,并建议增加密钥长度。增加密钥长度后,我能够验证令牌