简体   繁体   English

如何在Web Api C#中加密JWT令牌?

[英]How can I encrypt JWT Token in Web Api C#?

My Jwt Token looks like below: 我的Jwt令牌如下所示:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUuY29tIiwiaWF0IjoxMzU2OTk5NTI0LCJuYmYiOjEzNTcwMDAwMDAsImV4cCI6MTQwNzAxOTYyOSwianRpIjoiaWQxMjM0NTYiLCJ0eXAiOiJodHRwczpcL1wvZXhhbXBsZS5jb21cL3JlZ2lzdGVyIiwidGVzdC10eXBlIjoiZm9vIn0.UGLFIRACaHpGGIDEEv-4IIdLfCGXT62X1vYx7keNMyc

If I Copy the above JWT token and paste it in https://jwt.io/ then the actual information is showing like this 如果我复制上面的JWT令牌并将其粘贴到https://jwt.io/中,那么实际信息将像这样显示

Image 图片

I want to encrypt the JWT token with no one can access my information in Web Api. 我想加密JWT令牌,没有人可以访问Web Api中的信息。 So, I want to use any encryption algorithm, If so please provide me proper solution 因此,我想使用任何加密算法,如果可以,请提供适当的解决方案

Here is my Startup.cs file to authorize JWT Token. 这是我的授权JWT令牌的Startup.cs文件。

        // Api controllers with an [Authorize] attribute will be validated with JWT

        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = new[] { audience },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
                }
            });

    }

Why exactly do you think that it is a problem that someone can see this information? 您为什么确切认为有人可以看到此信息是一个问题? If you are using JWT for authentication, then it's only the user who was just authenticated will get this token and it probably contains the information about this user. 如果您使用JWT进行身份验证,则只有刚刚通过身份验证的用户才能获得此令牌,并且该令牌可能包含有关该用户的信息。 So it does not hurt for a user to be able to see information about himself. 因此,对于用户而言,能够看到有关他自己的信息不会有任何伤害。

Most importantly the token is mac'ed so that no one can forge it and pretend to be another user and that should be sufficient. 最重要的是,令牌被Maceded了,因此没有人可以伪造它并假装自己是另一个用户,这应该足够了。

And as mentioned in the comments you should be using TLS anyway, so it's encrypted in the transition. 就像评论中提到的那样,您还是应该使用TLS,因此在过渡过程中会对其进行加密。

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

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