Bewerken

Delen via


ClientFormsAuthenticationMembershipProvider.ValidateUser Method

Definition

Authenticates a user by using the specified credentials.

Overloads

ValidateUser(String, String)

Authenticates a user by using the specified user name and password.

ValidateUser(String, String, Boolean)

Authenticates a user by using the specified user name and password, optionally storing a hash of the password in the local data cache.

ValidateUser(String, String, String)

Authenticates a user at the specified service URI by using the specified user name and password.

ValidateUser(String, String)

Authenticates a user by using the specified user name and password.

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

Parameters

username
String

The name of the user to authenticate, or Empty or null to retrieve credentials from the IClientFormsAuthenticationCredentialsProvider implementation that this application is configured to use.

password
String

The password of the user to authenticate.

Returns

true if the user was authenticated; otherwise, false.

Exceptions

The IsOffline property value is false and the membership provider is unable to access the authentication service.

Examples

The following example code demonstrates how to use this method to validate the user by using an IClientFormsAuthenticationCredentialsProvider implementation. This example requires that you to configure your application to use a credentials provider.

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

Remarks

You can use client application services to validate users by using forms authentication. To validate users, you will typically call the static Membership.ValidateUser method, which internally calls the ClientFormsAuthenticationMembershipProvider.ValidateUser method. Alternatively, you can call this method directly, as shown in the Example section.

Forms authentication requires that the user specify their credentials through login controls provided by your application. You can retrieve the credentials and pass them to the Membership.ValidateUser method. You can also pass in empty strings or null to use a credentials provider.

See also

Applies to

ValidateUser(String, String, Boolean)

Authenticates a user by using the specified user name and password, optionally storing a hash of the password in the local data cache.

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

Parameters

username
String

The name of the user to authenticate.

password
String

The password of the user to authenticate.

rememberMe
Boolean

true to store a hash of the password in the local data cache for offline use and for automatic reauthentication when the user authentication cookie expires; false to disable offline login or to require users to reauthenticate when the cookie expires.

Returns

true if the user was authenticated; otherwise, false.

Exceptions

The IsOffline property value is false and the membership provider is unable to access the authentication service.

Examples

The following example code demonstrates how to use this method to validate the user by using login controls in your application code. This example requires a TextBox control named usernameTextBox, a TextBox control named passwordTextBox, and a CheckBox control named 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

Remarks

You can use client application services to validate users by using forms authentication. To validate users, you will typically call the static Membership.ValidateUser method, which internally calls the ClientFormsAuthenticationMembershipProvider.ValidateUser(String, String) method. Alternatively, you can call the ClientFormsAuthenticationMembershipProvider.ValidateUser method directly. You can call this overload to pass in a rememberMe value in addition to the username and password values.

See also

Applies to

ValidateUser(String, String, String)

Authenticates a user at the specified service URI by using the specified user name and password.

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

Parameters

username
String

The name of the user to authenticate.

password
String

The password of the user to authenticate.

serviceUri
String

The URI of the authentication service to use.

Returns

true if the user was authenticated; otherwise, false.

Exceptions

The IsOffline property value is false and the membership provider is unable to access the authentication service.

Examples

The following example code demonstrates how to use this method to validate the user through an authentication service at a specified location. The user credentials are retrieved from login controls in your application code. This example requires a TextBox control named usernameTextBox and a TextBox control named 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

Remarks

You can use client application services to validate users by using forms authentication. To validate users, you will typically call the static Membership.ValidateUser method, which internally calls the ClientFormsAuthenticationMembershipProvider.ValidateUser method. Alternatively, you can call the ClientFormsAuthenticationMembershipProvider.ValidateUser method directly. You can call this overload to access an authentication service at the location specified by the serviceUri parameter. Using this overload is an alternative to setting the ServiceUri property and calling the ValidateUser(String, String) overload.

See also

Applies to