简体   繁体   中英

How to hide external API token at .Net

I am working on an .net 5 web api projectt. I needed to use an external api (rapidapi.com etc.) . These websites gave me a apitoken. I know i should hide it , so no one at github would see it.

But also i should sent my apikey to my teacher so he could acces to, or hecan acces with his own apikey.

1.where sholud i store it? 2.How Should i store it? 3.How can i call it?

I know how to do it at JS, (its so easy, create .env file, and call it einvironmet.apikey ) , but don't know at .net. I need a simple solution like js

EDİT: I found a solution. you can add a section at appsettings.json like this

"API_KEY": "blablabla"

then use this in your controller private readonly IConfiguration _configuration;

create ctor for this

public CurrencyCalculatorAPI(IConfiguration configuration)
        {
            _configuration = configuration;
        }

then use your apikey like this

var apiKey = _configuration.GetValue("API_KEY");

you can use it wherever you want

You should use "User secrets", as described by Microsoft .

It's very easy in Visual Studio where you just right click your project and choose "Manage User Secrets". This will create a file in this location: %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json

This file is outside your project so it will never be a part of what you have in Github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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