Share via


ClientFormsAuthenticationMembershipProvider.ValidateUser 方法

定义

使用指定的凭据对用户进行身份验证。

重载

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 凭据提供程序。

另请参阅

适用于

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 的控件、TextBox名为 usernameTextBoxpasswordTextBox控件和名为 CheckBoxrememberMeCheckBox控件。

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 方法。 可以调用此重载来传入 rememberMe 除 和 password 值外username的值。

另请参阅

适用于

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,并且成员资格提供程序无法访问身份验证服务。

示例

以下示例代码演示如何使用此方法通过指定位置的身份验证服务验证用户。 从应用程序代码中的登录控件检索用户凭据。 此示例需要名为 TextBoxusernameTextBox 控件和 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) 替代方法。

另请参阅

适用于