0

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

0

4 Answers 4

9

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(); }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try with WebConfigurationManager :

WebConfigurationManager.AppSettings["username"];

1 Comment

in the aspx.cs file do samething ? isnt there any brackets in WebConfigurationManager ? eg [WebConfigurationManager].AppSetting["username"] ?
1

Its not safe to hard code the values to web.config file.For passwords store the Hash Value Instead of Original string.Also try to use the DPAPI.

Comments

-1

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;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.