我在我的 web.config 文件中添加了以下设置,以启动对外部系统的 API 调用。所以我将 API URL + 用户名 + 密码存储如下:-
<appSettings>
<add key="ApiURL" value="https://...../servlets/AssetServlet" />
<add key="ApiUserName" value="tmsservice" />
<add key="ApiPassword" value="test2test2" />
然后在我的操作方法中,我将在构建 Web 客户端时引用这些值,如下所示:-
public ActionResult Create(RackJoin rj, FormCollection formValues)
{
XmlDocument doc = new XmlDocument();
using (var client = new WebClient())
{
var query = HttpUtility.ParseQueryString(string.Empty);
foreach (string key in formValues)
{
query[key] = this.Request.Form[key];
}
query["username"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];
query["password"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiPassword"];
string apiurl = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiURL"];
但是我将公开用户名和密码,这些可以被用户捕获,所以我的问题是如何保护 API 用户名和密码?
最佳答案
您可以 encrypt the web.config with aspnet_regiis .这是为了阻止有权访问您的服务器的人阅读敏感信息。
顺便说一下,我会将您的配置设置放在一个类中,然后可以将其注入(inject)到您的 Controller 中 - 这将使单元测试更加容易。
关于c# - 如何保护存储在 web.config 中的密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20908438/