简体   繁体   English

如何知道 JWT 的密钥以及在 windows 服务器上的存储位置

[英]How to know the secret key of JWT and where to store it on windows server

I am trying to implement JWT authentication in my Web API.我正在尝试在我的 Web API 中实现 JWT 身份验证。 I am doing this for the first time.我是第一次这样做。 When I Googled some tutorial, the first step is showing up to save secret key in appsettings.json file.当我在谷歌上搜索一些教程时,第一步是显示将密钥保存在 appsettings.json 文件中。 I am stuck at that place itself.我被困在那个地方本身。 How will I know and get that secret key so that I can store it in a file.我将如何知道并获取该密钥,以便将其存储在文件中。 Also how will I know secret key for my each environment ie Dev, TEST and Prod.另外,我将如何知道我的每个环境(即 Dev、TEST 和 Prod)的密钥。

Thanks in advance.提前致谢。

Here, first we have configure our key in appsettings.json.在这里,首先我们在 appsettings.json 中配置我们的密钥。 That might be any thing with atleast 16 characters.这可能是至少有 16 个字符的任何东西。

In Appsettings.json file, add configuration as mentioned below, "Appsettings":{"key":"this is my keyss"}在 Appsettings.json 文件中,添加如下所述的配置, "Appsettings":{"key":"this is my keyss"}

In the constructor of the controller class, initialize the configuration,在控制器类的构造函数中,初始化配置,

private readonly IConfiguration _config;
    public UserAuth(IConfiguration config){
                _config=config;
            }

In the place where you want that key for signing purpose, we can retrive by the following code,在您想要该密钥用于签名的地方,我们可以通过以下代码进行检索,

var key=new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_config.GetSection("Appsettings:key").Value));

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

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