admin管理员组

文章数量:1405908

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,

本文标签: How to store secure variable values in http files in Visual StudioStack Overflow