ConnectionString loses password after connection.Open

后端 未结 3 1934
醉梦人生
醉梦人生 2020-12-07 16:26

i\'m using ADO.NET to get some information from the database on a server,
so this is what i do:

string conStr = \"Data Source=myServer\\SQLEXPRESS;Initia         


        
相关标签:
3条回答
  • 2020-12-07 17:13

    You may want to add your own validation but this will take a standard SqlConnection (without persist security) and access the private ConnectionOptions property to retrieve the connection string.

    public static string SqlConnectionToConnectionString(SqlConnection conn)
    {
        System.Reflection.PropertyInfo property = conn.GetType().GetProperty("ConnectionOptions", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        object optionsObject = property.GetValue(conn, null);
        System.Reflection.MethodInfo method = optionsObject.GetType().GetMethod("UsersConnectionString");
        string connStr = method.Invoke(optionsObject, new object[] { false }) as string; // argument is "hidePassword" so we set it to false
        return connStr;
    }
    

    Note that this might break if MS change the underlying implementation, since we're using reflection. I'm not advising this as the best way to do it, but it's a way.

    0 讨论(0)
  • 2020-12-07 17:18

    This is by design, for security reasons. From MSDN:

    The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

    0 讨论(0)
  • 2020-12-07 17:20

    Look in the connection string, in order to keep the password in the ConnectionString property you must add "Persist Security Info=true;" to the connection string itself.

    The following example will strip the password out:

    string conStr = "Data Source=localhost;Initial Catalog=MyDatabase;User Id=MyUser;Password=MyPassword";
    SqlConnection conn = new SqlConnection(conStr);
    conn.Open();
    conn.Close();
    Console.WriteLine(conn.ConnectionString);
    

    The following example will keep the password in the conn.ConnectionString:

    string conStr = "Persist Security Info=True;Data Source=localhost;Initial Catalog=MyDatabase;User Id=MyUser;Password=MyPassword";
    SqlConnection conn = new SqlConnection(conStr);
    conn.Open();
    conn.Close();
    Console.WriteLine(conn.ConnectionString);
    

    Its a property set inside the connection string itself not in the SqlConnection object, I put it in the beginning of the connection string just so you don't have to scroll to see it, it can go anywhere in the connection string, I usually see it at the end.

    Like others have said, if you need to do this you quite possibly are not using the SqlConnection object exactly as it was intended.

    0 讨论(0)
提交回复
热议问题