vb.net 从 web.config 存储和检索值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/784890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:11:33  来源:igfitidea点击:

Store and Retrieve values from web.config

vb.netweb-configasp.net-3.5

提问by Maen

I built a small website and there will be only one admin, so in the admin panel I am asking for a password with a value that I do not retrieve from a database, I just hard coded it in the function in code behind, I know this is wrong though I don't know why.

我建立了一个小网站,只有一个管理员,所以在管理面板中,我要求输入一个密码,该密码的值不是从数据库中检索的,我只是在后面的代码中将其硬编码在函数中,我知道这是错误的,虽然我不知道为什么。

So is hard coding it in web.config the right thing to do? and how?

那么在 web.config 中对其进行硬编码是正确的做法吗?如何?

回答by Eoin Campbell

As far as it being wrong... the problem is that if you ever need to change it, and it's hardcoded in your codebehind, you need to recompile,republish, re-deploy your website, whereas a change to the web.config can be done without doing this.

至于它是错误的……问题是,如果您需要更改它,并且它是硬编码在您的代码隐藏中,您需要重新编译、重新发布、重新部署您的网站,而对 web.config 的更改可以不这样做就可以完成。

You could put it in an AppSetting in the web.config like so.

你可以像这样把它放在 web.config 的 AppSetting 中。

<appSettings>
   <add key="AdminPassword" value="ASDF1234" />
</appSettings>

and use this code to retrieve it

并使用此代码检索它

System.Configuration.ConfigurationManager.AppSettings["AdminPassword"].ToString()

Though I'd have a look at this.

虽然我会看看这个。

http://aspnet.4guysfromrolla.com/articles/021506-1.aspx

http://aspnet.4guysfromrolla.com/articles/021506-1.aspx

It covers encrypting sections of your web.config

它涵盖了 web.config 的加密部分

回答by Chad Grant

Nothing wrong with Eoin's suggestion for tiny projects but if your project may someday need more than 1 admin and different types of users roles. I would take the hit and setup ASP membership.

Eoin 对小型项目的建议没有错,但如果您的项目有一天可能需要超过 1 个管理员和不同类型的用户角色。我会接受打击并设置 ASP 会员资格。

http://msdn.microsoft.com/en-us/library/ms998347.aspx

http://msdn.microsoft.com/en-us/library/ms998347.aspx

You can use integrate it into windows or use a database and it's not too hard to setup. Especially if you use the built in config tool in IIS.

您可以将其集成到 Windows 中或使用数据库,并且设置起来并不难。特别是如果您使用 IIS 中的内置配置工具。