Export (0) Print
Expand All

ClientFormsAuthenticationMembershipProvider.ValidateUser Method (String, String)

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

Namespace:  System.Web.ClientServices.Providers
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public override bool ValidateUser(
	string username,
	string password
)

Parameters

username
Type: System.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
Type: System.String

The password of the user to authenticate.

Return Value

Type: System.Boolean
true if the user was authenticated; otherwise, false.

ExceptionCondition
WebException

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

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. For more information, see How to: Implement User Login with Client Application Services.

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. For more information, see How to: Configure Client Application Services.

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;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5
Show:
© 2015 Microsoft