Add SqlCredential support -
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlConnection.cs
index 8712eca6cf9300024b0ccf802a5738eb96df6324..e1fa878c4caa3045bcd95254ba1b2c015ce3370b 100644 (file)
@@ -56,6 +56,7 @@ using System.Xml;
 #if NET_2_0
 using System.Collections.Generic;
 #endif
+using System.Security;
 
 namespace System.Data.SqlClient
 {
@@ -93,6 +94,9 @@ namespace System.Data.SqlClient
                // The connection string that identifies this connection
                string connectionString;
 
+               // The connection credentials
+               SqlCredential credentials;
+
                // The transaction object for the current transaction
                SqlTransaction transaction;
 
@@ -133,6 +137,12 @@ namespace System.Data.SqlClient
                        ConnectionString = connectionString;
                }
 
+               public SqlConnection (string connectionString, SqlCredential cred)
+               {
+                       ConnectionString = connectionString;
+                       Credentials = cred;
+               }
+
                #endregion // Constructors
 
                #region Properties
@@ -155,6 +165,15 @@ namespace System.Data.SqlClient
                        }
                }
        
+               public SqlCredential Credentials {
+                       get {
+                               return credentials;
+                       }
+                       set {
+                               credentials = value;
+                       }
+               }
+       
 #if !NET_2_0
                [DataSysDescription ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")] 
 #endif
@@ -563,6 +582,16 @@ namespace System.Data.SqlClient
 
                        if (!tds.IsConnected) {
                                try {
+                                       if (Credentials != null) {
+                                               if (parms.User != String.Empty)
+                                                       throw new ArgumentException("UserID already specified");
+                                               if (parms.PasswordSet)
+                                                       throw new ArgumentException("Password already specified");
+                                               if (parms.DomainLogin != false)
+                                                       throw new ArgumentException("Cannot use credentials with DomainLogin");
+                                               parms.User = Credentials.UserId;
+                                               parms.Password = Credentials.Password;
+                                       }
                                        tds.Connect (parms);
                                } catch {
                                        if (pooling)
@@ -879,7 +908,10 @@ namespace System.Data.SqlClient
                                break;
                        case "password" :
                        case "pwd" :
-                               parms.Password = value;
+                               parms.Password = new SecureString();
+                               foreach (char c in value)
+                                       parms.Password.AppendChar(c);
+                               parms.PasswordSet = true;
                                break;
                        case "persistsecurityinfo" :
                        case "persist security info" :