简体   繁体   中英

Hard coded values to web.config file

i already have a phpcode with hard coded values,

$username = "abcd";
$password = "abcd123";

now i wanted to put those values to web.config file.here is my work,but something wrong here.

<appSettings>
<add key="username" username="abcd"/>
<add key="password" password="abcd123"/>
<appSettings/>

so.. is there any problem ? and i also wanted to know how can i take this settings to aspx.cs file.. i mean [configurationmanager] something

You need to declare them with the key and value properties instead, like so:

<appSettings>
   <add key="username" value="abcd"/>
   <add key="password" value="abcd123"/>
<appSettings/>

If you want the basics, you can access the keys via:

string username = System.Configuration.ConfigurationManager.AppSettings["username"].ToString();
string password = System.Configuration.ConfigurationManager.AppSettings["password"].ToString();

To access my web config keys I always make a static class in my application. It means I can access them wherever I require and I'm not using the strings all over my application (if it changes in the web config I'd have to go through all the occurrences changing them). Here's a sample:

using System.Configuration;

public static class AppSettingsGet
{    
    public static string Username
    {
        get { return ConfigurationManager.AppSettings["username"].ToString(); }
    }

    public static string Password
    {
        get { return ConfigurationManager.AppSettings["password"].ToString(); }
    }
}

尝试使用WebConfigurationManager

WebConfigurationManager.AppSettings["username"];

将值硬编码到web.config文件中是不安全的。对于密码,请存储哈希值而不是原始字符串。也请尝试使用DPAPI。

When thosethings done.. after that is needed below things to do ? here i create a instance.or how to access/call web.config's connection string in my page.

 internal static ConnectionFactory newinstance()
    {
        try
        {
            return new   ConnectionFactory(ConfigurationManager.ConnectionStrings["myConString"].ConnectionString);
        }
        catch (Exception)
        {
            throw;
        }

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