最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to store secure variable values in .http files in Visual Studio? - Stack Overflow

programmeradmin0浏览0评论

I need to store a username and password in my .http file in Visual Studio in a secure and easy way so that it is not checked into source control accidentally.

@MyAuth.Auth_HostAddress = http://localhost:5085

POST {{MyAuth.Auth_HostAddress}}/api/v1/auth/login
Content-Type: application/json
{
    "username": "myusername",
    "password": "mypassword"
}

I need to store a username and password in my .http file in Visual Studio in a secure and easy way so that it is not checked into source control accidentally.

@MyAuth.Auth_HostAddress = http://localhost:5085

POST {{MyAuth.Auth_HostAddress}}/api/v1/auth/login
Content-Type: application/json
{
    "username": "myusername",
    "password": "mypassword"
}
Share Improve this question asked Mar 7 at 6:38 Rizan ZakyRizan Zaky 4,6925 gold badges29 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In Visual Studio you can achieve this using environment files, specifically user-specific environment files.

To start with,

  1. Add a file called http-client.env.json in the same directory as the .http file. By default, this file is set to check into source control.
    This file can be left blank, if you don't have any values checked in.

    { }

  2. Add another file called http-client.env.json.user in the same directory. This is where the secret values go in. By default, this file is set not to check into source control.
    In this file you can set the secrets under an environment root,

    {
      "dev": {
        "Username": "myusername1",
        "Password": "mypassword1"
      },
      "prod": {
        "Username": "myusername2",
        "Password": "mypassword2"
      }
    }
    
  3. Now in the .http file access the defined variable using {{ }},
    In this example, the environment variable file values are read into .http file variables,

    @UsernameVar = {{Username}}
    @PasswordVar = {{Password}}
    

    then used as needed,

    POST {{MyAuth.Auth_HostAddress}}/api/v1/auth/login
    Content-Type: application/json
    {
        "username": "{{UsernameVar}}",
        "password": "{{PasswordVar}}"
    }
    
  4. Finally you can switch environments from the defined list of environemnts in the top right corner of Visual Studio .http file editor,

发布评论

评论列表(0)

  1. 暂无评论