ערוך

שתף באמצעות


ClientFormsAuthenticationMembershipProvider.Logout Method

Definition

Logs out the user.

public:
 void Logout();
public void Logout ();
member this.Logout : unit -> unit
Public Sub Logout ()

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 log out the user.

private void logoutButton_Click(object sender, EventArgs e)
{
    SaveSettings();

    ClientFormsAuthenticationMembershipProvider authProvider =
        (ClientFormsAuthenticationMembershipProvider)
        System.Web.Security.Membership.Provider;

    try
    {
        authProvider.Logout();
    }
    catch (WebException)
    {
        MessageBox.Show("Unable to access the authentication service." +
            Environment.NewLine + "Logging off locally only.",
            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        ConnectivityStatus.IsOffline = true;
        authProvider.Logout();
        ConnectivityStatus.IsOffline = false;
    }

    Application.Restart();
}
Private Sub logoutButton_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles logoutButton.Click

    SaveSettings()

    Dim authProvider As ClientFormsAuthenticationMembershipProvider = _
        CType(System.Web.Security.Membership.Provider,  _
        ClientFormsAuthenticationMembershipProvider)

    Try

        authProvider.Logout()

    Catch ex As WebException

        MessageBox.Show("Unable to access the authentication service." & _
            Environment.NewLine & "Logging off locally only.", _
            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ConnectivityStatus.IsOffline = True
        authProvider.Logout()
        ConnectivityStatus.IsOffline = False

    End Try

    Application.Restart()

End Sub

Remarks

The Logout method clears all authentication cookies from the cookie cache and resets the static Thread.CurrentPrincipal property to a WindowsPrincipal object that contains the current WindowsIdentity.

After you call this method, the current user is no longer authenticated for client application services. This means that you cannot retrieve roles through the ClientRoleProvider class and settings through the ClientSettingsProvider class. However, because the user might have a valid Windows identity, you might still receive a true value from code such as the following: Thread.CurrentPrincipal.Identity.IsAuthenticated. To determine whether the user is authenticated for client application services, confirm that the Identity property value of the IPrincipal retrieved through the static CurrentPrincipal property is a ClientFormsIdentity reference. Then, check the ClientFormsIdentity.IsAuthenticated property.

To reauthenticate the current user, call the ClientFormsAuthenticationMembershipProvider.ValidateUser method or the static Membership.ValidateUser method.

Applies to

See also