Hiding private details from open source projects

后端 未结 4 512
轻奢々
轻奢々 2021-02-11 07:39

I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do

4条回答
  •  我寻月下人不归
    2021-02-11 08:25

    This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.

    You can use the appSettings section to store your API key.

    To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.

    Modify your web.config (or app.config):

    
    
    
    
    
    
    
    

    Create a new specialappsettings.config file:

    
    
    
    
    

    This can be accessed in your code via:

    var apiKey = ConfigurationManager.AppSettings["APIKey"];
    

    Notes:

    • You can keep your settings within the original web.config file as well but this lets you ignore just the specific settings file from your git repository without affecting the rest of the project's necessary configuration details.
    • The same "key" can be saved in either file however the external file will override the original web.config file value.

提交回复
热议问题