ClientFormsAuthenticationMembershipProvider.ValidateUser 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用指定的認證來驗證使用者。
多載
| 名稱 | Description |
|---|---|
| ValidateUser(String, String) |
使用指定的使用者名稱和密碼來驗證使用者。 |
| ValidateUser(String, String, Boolean) |
使用指定的使用者名稱和密碼來驗證使用者,並選擇性地將密碼的哈希儲存在本機數據快取中。 |
| ValidateUser(String, String, String) |
使用指定的使用者名稱和密碼,在指定的服務 URI 上驗證使用者。 |
ValidateUser(String, String)
使用指定的使用者名稱和密碼來驗證使用者。
public:
override bool ValidateUser(System::String ^ username, System::String ^ password);
public override bool ValidateUser(string username, string password);
override this.ValidateUser : string * string -> bool
Public Overrides Function ValidateUser (username As String, password As String) As Boolean
參數
- username
- String
使用者名稱,用於認證或Emptynull擷取本應用程式所設定使用的實作憑證IClientFormsAuthenticationCredentialsProvider。
- password
- String
要驗證之用戶的密碼。
傳回
true若使用者已認證;否則,。 false
例外狀況
屬性值為 IsOffline , false 會員提供者無法存取認證服務。
範例
以下範例程式碼示範如何利用此方法透過實 IClientFormsAuthenticationCredentialsProvider 作來驗證使用者。 此範例會要求您設定應用程式以使用認證提供者。
private bool ValidateUsingCredentialsProvider()
{
bool isAuthorized = false;
try
{
ClientFormsAuthenticationMembershipProvider authProvider =
System.Web.Security.Membership.Provider as
ClientFormsAuthenticationMembershipProvider;
// Call ValidateUser with empty strings in order to display the
// login dialog box configured as a credentials provider.
isAuthorized = authProvider.ValidateUser(String.Empty, String.Empty);
}
catch (System.Net.WebException)
{
MessageBox.Show("Unable to access the authentication service.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (!isAuthorized)
{
MessageBox.Show("Unable to authenticate.", "Not logged in",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
return isAuthorized;
}
Private Function ValidateUsingCredentialsProvider() As Boolean
Dim isAuthorized As Boolean = False
Try
Dim authProvider As ClientFormsAuthenticationMembershipProvider = _
CType(System.Web.Security.Membership.Provider, _
ClientFormsAuthenticationMembershipProvider)
' Call ValidateUser with empty strings in order to display the
' login dialog box configured as a credentials provider.
isAuthorized = authProvider.ValidateUser(String.Empty, String.Empty)
Catch ex As System.Net.WebException
MessageBox.Show("Unable to access the authentication service.", _
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
If Not isAuthorized Then
MessageBox.Show("Unable to authenticate.", "Not logged in", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End If
Return isAuthorized
End Function
備註
您可以使用用戶端應用程式服務,使用窗體驗證來驗證使用者。 為了驗證使用者,通常會呼叫 staticMembership.ValidateUser 該方法,而該方法內部也會呼叫該 ClientFormsAuthenticationMembershipProvider.ValidateUser 方法。 或者,您可以直接呼叫此方法,如範例區段所示。
窗體驗證需要使用者透過應用程式所提供的登入控件來指定其認證。 你可以取得憑證並傳給 Membership.ValidateUser 方法。 你也可以輸入空字串,或 null 使用憑證服務提供者。
另請參閱
- IClientFormsAuthenticationCredentialsProvider
- ValidateUser(String, String)
- 客戶端應用服務
- 如何:配置客戶端應用服務
- 如何:與客戶應用服務(Client Application Services)實作使用者登入與登出
適用於
ValidateUser(String, String, Boolean)
使用指定的使用者名稱和密碼來驗證使用者,並選擇性地將密碼的哈希儲存在本機數據快取中。
public:
bool ValidateUser(System::String ^ username, System::String ^ password, bool rememberMe);
public bool ValidateUser(string username, string password, bool rememberMe);
override this.ValidateUser : string * string * bool -> bool
Public Function ValidateUser (username As String, password As String, rememberMe As Boolean) As Boolean
參數
- username
- String
要驗證的使用者名稱。
- password
- String
要驗證之用戶的密碼。
- rememberMe
- Boolean
true 將密碼的雜湊值儲存在本地資料快取中,供離線使用及使用者認證 Cookie 到期時自動重新認證; false 關閉離線登入或要求使用者在 Cookie 到期時重新驗證。
傳回
true若使用者已認證;否則,。 false
例外狀況
屬性值為 IsOffline , false 會員提供者無法存取認證服務。
範例
下列範例程式代碼示範如何使用此方法,在應用程式程式代碼中使用登入控件來驗證使用者。 此範例需要一個TextBox名為 usernameTextBox、 的控制TextBox點 ,passwordTextBox以及CheckBox一個名為 rememberMeCheckBox的控制點。
private bool ValidateUsingLoginControls()
{
bool isAuthorized = false;
try
{
ClientFormsAuthenticationMembershipProvider authProvider =
System.Web.Security.Membership.Provider as
ClientFormsAuthenticationMembershipProvider;
// Call ValidateUser with credentials retrieved from login controls.
isAuthorized = authProvider.ValidateUser(usernameTextBox.Text,
passwordTextBox.Text, rememberMeCheckBox.Checked);
}
catch (System.Net.WebException)
{
MessageBox.Show("Unable to access the authentication service.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (!isAuthorized)
{
MessageBox.Show("Unable to authenticate.", "Not logged in",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
return isAuthorized;
}
Private Function ValidateUsingLoginControls() As Boolean
Dim isAuthorized As Boolean = False
Try
Dim authProvider As ClientFormsAuthenticationMembershipProvider = _
CType(System.Web.Security.Membership.Provider, _
ClientFormsAuthenticationMembershipProvider)
' Call ValidateUser with credentials retrieved from login controls.
isAuthorized = authProvider.ValidateUser(usernameTextBox.Text, _
passwordTextBox.Text, rememberMeCheckBox.Checked)
Catch ex As System.Net.WebException
MessageBox.Show("Unable to access the authentication service.", _
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
If Not isAuthorized Then
MessageBox.Show("Unable to authenticate.", "Not logged in", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End If
Return isAuthorized
End Function
備註
您可以使用用戶端應用程式服務,使用窗體驗證來驗證使用者。 為了驗證使用者,通常會呼叫 staticMembership.ValidateUser 該方法,而該方法內部也會呼叫該 ClientFormsAuthenticationMembershipProvider.ValidateUser(String, String) 方法。 或者,你也可以直接打電話給這個 ClientFormsAuthenticationMembershipProvider.ValidateUser 方法。 你可以呼叫這個超載,除了 和 rememberMeusername 值之外,還會傳遞一個password值。
另請參閱
適用於
ValidateUser(String, String, String)
使用指定的使用者名稱和密碼,在指定的服務 URI 上驗證使用者。
public:
static bool ValidateUser(System::String ^ username, System::String ^ password, System::String ^ serviceUri);
public static bool ValidateUser(string username, string password, string serviceUri);
static member ValidateUser : string * string * string -> bool
Public Shared Function ValidateUser (username As String, password As String, serviceUri As String) As Boolean
參數
- username
- String
要驗證的使用者名稱。
- password
- String
要驗證之用戶的密碼。
- serviceUri
- String
要使用的驗證服務的 URI。
傳回
true若使用者已認證;否則,。 false
例外狀況
屬性值為 IsOffline , false 會員提供者無法存取認證服務。
範例
下列範例程式代碼示範如何使用此方法,透過指定位置的驗證服務來驗證使用者。 使用者認證會從應用程式程式代碼中的登入控件擷取。 此範例需要一個 TextBox 名為 usernameTextBox 的控制點 和 TextBox 一個名為 passwordTextBox的控制點。
private bool ValidateUsingServiceUri(String serviceUri)
{
bool isAuthorized = false;
try
{
// Call the static overload of ValidateUser. Specify credentials
// retrieved from login controls and the service location.
isAuthorized =
ClientFormsAuthenticationMembershipProvider.ValidateUser(
usernameTextBox.Text, passwordTextBox.Text, serviceUri);
}
catch (System.Net.WebException)
{
MessageBox.Show("Unable to access the authentication service.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (!isAuthorized)
{
MessageBox.Show("Unable to authenticate.", "Not logged in",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
return isAuthorized;
}
Private Function ValidateUsingServiceUri(ByVal serviceUri As String) As Boolean
Dim isAuthorized As Boolean = False
Try
' Call the Shared overload of ValidateUser. Specify credentials
' retrieved from login controls and the service location.
isAuthorized = _
ClientFormsAuthenticationMembershipProvider.ValidateUser( _
usernameTextBox.Text, passwordTextBox.Text, serviceUri)
Catch ex As System.Net.WebException
MessageBox.Show("Unable to access the authentication service.", _
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
If Not isAuthorized Then
MessageBox.Show("Unable to authenticate.", "Not logged in", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End If
Return isAuthorized
End Function
備註
您可以使用用戶端應用程式服務,使用窗體驗證來驗證使用者。 為了驗證使用者,通常會呼叫 staticMembership.ValidateUser 該方法,而該方法內部也會呼叫該 ClientFormsAuthenticationMembershipProvider.ValidateUser 方法。 或者,你也可以直接打電話給這個 ClientFormsAuthenticationMembershipProvider.ValidateUser 方法。 你可以呼叫這個超載,以存取參數指定 serviceUri 位置的認證服務。 使用這種過載是設定 ServiceUri 屬性並呼叫 ValidateUser(String, String) 過載的替代方案。