读取用户名和密码值配置值而不是类中的硬编码

时间:2017-05-25 22:54:24

标签: vb.net encryption connection-string config

我有配置文件

<appSettings>
    <add key="UserName" value="encryptedusername"/>
    <add key="Password" value="encryptedpassword"/>  
</appSettings>

我想解密它们并在我的方法中使用它们。目前它是硬编码的。我想从配置中获取值并删除代码中的硬编码。如何更改方法:

Private Function GetConnectionStringFromWebConfig() As String
    Dim strConnection As String
    'Dim intEnviroCount As Integer = 
    'ConfigurationSettings.AppSettings("EnvironmentCount")
    Dim i As Integer
    Dim strServer As String
    Dim strCatalog As String
    Dim strUserName As String
    Dim strPassword As String

    strServer = _SqlServer
    strCatalog = _Catalog
    strUserName = _UserName
    strPassword = _Password

    ' Add in required fields
    strConnection = "Data Source=" & strServer & "; Initial Catalog=" & strCatalog & "; Max Pool Size=500;"

    ' Add in (optional) user field
    If strUserName = "" Then
      strConnection &= "User ID=hardcodedusername;"
    Else
      strConnection &= "User ID=" & strUserName & ";"
    End If

    ' Add in (optional) password field
    If strPassword = "" Then
      strConnection &= "Password=Hardcodedpassword;"
    Else
      strConnection &= "Password=" & strPassword & ";"
    End If

    _strServer = strServer
    _strCatalog = strCatalog

    Return strConnection
End Function

我正在尝试使用

Dim UserPane As String = System.Configuration.ConfigurationManager.AppSettings["username"].ToString();

我收到以下错误:

namevaluecollection can not be converted to string

0 个答案:

没有答案