SqlCredential 类
定义
SqlCredential 提供了更安全的方式来指定使用 SQL Server 身份验证尝试登录的密码。SqlCredential provides a more secure way to specify the password for a login attempt using SQL Server Authentication.
SqlCredential 由用户 ID 和将用于 SQL Server 身份验证的密码构成。SqlCredential is comprised of a user id and a password that will be used for SQL Server Authentication. SqlCredential 对象中的密码是 SecureString 类型。The password in a SqlCredential object is of type SecureString.
无法继承 SqlCredential。SqlCredential cannot be inherited.
Windows 身份验证 (Integrated Security = true) 仍是登录到 SQL Server 数据库的最安全的方式。Windows Authentication (Integrated Security = true) remains the most secure way to log in to a SQL Server database.
public ref class SqlCredential sealed
public sealed class SqlCredential
[System.Serializable]
public sealed class SqlCredential
type SqlCredential = class
[<System.Serializable>]
type SqlCredential = class
Public NotInheritable Class SqlCredential
- 继承
-
SqlCredential
- 属性
注解
用于 Credential 获取或设置连接的 SqlCredential 对象。Use Credential to get or set a connection's SqlCredential object. 使用 ChangePassword 通过对象更改用户的密码 SqlCredential 。Use ChangePassword to change the password for a user via an SqlCredential object. 有关 SqlCredential 对象如何影响连接池行为的信息,请参阅 SQL Server 连接池 (ADO.NET) 。For information on how a SqlCredential object affects connection pool behavior, see SQL Server Connection Pooling (ADO.NET).
InvalidOperationException如果使用 SqlCredential 以下任何连接字符串关键字的连接中使用了非 null 对象,则将引发异常:An InvalidOperationException exception will be raised if a non-null SqlCredential object is used in a connection with any of the following connection string keywords:
Integrated Security = truePasswordUser IDContext Connection = true
下面的示例使用连接到 SQL Server 数据库 Credential :The following sample connects to a SQL Server database using Credential:
// change connection string in the APP.CONFIG file
<connectionStrings>
<add name="MyConnString"
connectionString="Initial Catalog=myDB;Server=myServer"
providerName="System.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();
构造函数
| SqlCredential(String, SecureString) |
创建一个 SqlCredential 类型的对象。Creates an object of type SqlCredential. |
属性
| Password |
获取 SqlCredential 对象的密码组件。Gets the password component of the SqlCredential object. |
| UserId |
获取 SqlCredential 对象的用户 ID 组件。Gets the user ID component of the SqlCredential object. |
方法
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |