SqlCredential Klasa

Definicja

SqlCredentialzapewnia bardziej bezpieczny sposób określania hasła do próby logowania przy użyciu uwierzytelniania SQL Server.

SqlCredentialskłada się z identyfikatora użytkownika i hasła, które będą używane do uwierzytelniania SQL Server. Hasło w SqlCredential obiekcie ma typ SecureString.

SqlCredential nie można dziedziczyć.

Uwierzytelnianie systemu Windows (Integrated Security = true) pozostaje najbezpieczniejszym sposobem logowania się do bazy danych SQL Server.

public ref class SqlCredential sealed
public sealed class SqlCredential
type SqlCredential = class
Public NotInheritable Class SqlCredential
Dziedziczenie
SqlCredential

Uwagi

Użyj polecenia Credential , aby pobrać lub ustawić obiekt połączenia SqlCredential . Użyj ChangePassword polecenia , aby zmienić hasło dla SqlCredential obiektu. Aby uzyskać informacje na temat SqlCredential wpływu obiektu na zachowanie puli połączeń, zobacz SQL Server Łączenie puli połączeń (ADO.NET).

Wyjątek InvalidOperationException zostanie zgłoszony, jeśli obiekt inny niż null SqlCredential jest używany w połączeniu z dowolnym z następujących słów kluczowych parametrów połączenia:

  • Integrated Security = true

  • Password

  • User ID

Poniższy przykład łączy się z bazą danych SQL Server przy użyciu polecenia Credential:

// change connection string in the APP.CONFIG file  
<connectionStrings>  
  <add name="MyConnString"  
       connectionString="Initial Catalog=myDB;Server=myServer"  
       providerName="Microsoft.Data.SqlClient" />  
</connectionStrings>  

// then use the following snippet:  
using System.Configuration;  

System.Windows.Controls.TextBox txtUserId = new System.Windows.Controls.TextBox();  
System.Windows.Controls.PasswordBox txtPwd = new System.Windows.Controls.PasswordBox();  

Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration(Null);  
ConnectionStringSettings connString = config.ConnectionStrings.ConnectionString["MyConnString"];  

using (SqlConnection conn = new SqlConnection(connString.ConnectionString))  
{  
    SecureString pwd = txtPwd.SecurePassword;  
    pwd.MakeReadOnly();  
    SqlCredential cred = new SqlCredential(txtUserId.Text, pwd);  
    conn.Credential = cred;  
    conn.Open();  
}

Konstruktory

SqlCredential(String, SecureString)

Tworzy obiekt typu SqlCredential.

Właściwości

Password

Pobiera składnik SqlCredential hasła obiektu.

UserId

Pobiera składnik SqlCredential identyfikatora użytkownika obiektu.

Dotyczy