在C#控制台应用程序app.config中嵌入Webclient的凭据? [英] Embed credentials for webclient in C# Console Application app.config?

查看:35
本文介绍了在C#控制台应用程序app.config中嵌入Webclient的凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我正在使用类似这样的登录方法进行登录,但是我想将其嵌入到app.config文件中以保持安全...

Currently I'm using a login method like this to login, but I want to embed this into the app.config file for safe keeping...

当前看起来像这样.

WebClient c = new WebClient();  
c.Credentials = new System.Net.NetworkCredential("UN", "PW", "server");

如何将其嵌入到app.config文件中?

How can I embed this into the app.config file???

推荐答案

<appSettings>
  <add key="NetworkCredentialUserName" value="UN" />
  <add key="NetworkCredentialPassword" value="PW" />
</appSettings>

然后

var networkCredentialUserName = ConfigurationManager.AppSettings["NetworkCredentialUserName"];
var networkCredentialPassword = ConfigurationManager.AppSettings["NetworkCredentialPassword"];
c.Credentials = new System.Net.NetworkCredential(networkCredentialUserName, networkCredentialPassword , "server");

但是,这不能确保它们的安全.如果网络凭据用户名&密码很敏感,我会考虑使用PKCS12ProtectedConfigurationProvider(一个nuget包)之类的东西对app.config appSettings部分进行加密.

This however does not make them secure. If the network credential username & password are sensitive, I would consider encrypting the app.config appSettings section using something like PKCS12ProtectedConfigurationProvider (a nuget package).

这篇关于在C#控制台应用程序app.config中嵌入Webclient的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆