2012-06-01 48 views
0

我已經有一個硬編碼值phpcode,硬編碼值web.config文件

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

現在我希望把這些值來web.config中file.here是我的工作,但事情錯在這裏。

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

so ..有什麼問題嗎?我也想知道我怎麼可以把這個設置aspx.cs文件..我的意思[ConfigurationManager中]東西

+0

您正在使用的WinForms – techno

+0

no.web應用 – TechGuy

回答

9

您需要具有鍵和值的屬性來聲明他們,而不是像這樣:

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

如果你想要的基本知識,您可以通過訪問鍵:

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

要訪問我的web配置鑰匙,我總是做一個靜態類在我的應用程序。這意味着我可以在任何需要的地方訪問它們,而且我不會在整個應用程序中使用字符串(如果它在Web配置中發生更改,我將不得不通過所有更改它們的事件)。這裏有一個例子:

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(); } 
    } 
} 
2

嘗試用WebConfigurationManager

WebConfigurationManager.AppSettings["username"]; 
+0

在aspx.cs文件做samething? WebConfigurationManager中沒有任何括號?例如[WebConfigurationManager] .AppSetting [「username」]? – TechGuy

+0

這是C#代碼... – aleroot

1

它不是安全的硬編碼值來web.config中file.For存儲密碼散列值而不是原始string.Also的嘗試使用DPAPI。

-1

當thosethings完成..之後需要做的事情?在這裏我創建一個instance.or如何訪問/在我的頁面中調用web.config的連接字符串。

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