ClientFormsAuthenticationMembershipProvider.Logout 方法

定义

注销用户。

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

例外

IsOffline 属性值为 false,并且成员资格提供程序无法访问身份验证服务。

示例

以下示例代码演示如何使用此方法注销用户。

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

注解

方法 Logout 从 Cookie 缓存中清除所有身份验证 Cookie,并将 属性重置 staticThread.CurrentPrincipalWindowsPrincipal 包含当前 WindowsIdentity的 对象。

调用此方法后,当前用户不再对客户端应用程序服务进行身份验证。 这意味着无法通过 类检索角色, ClientRoleProvider 而无法通过 ClientSettingsProvider 类检索设置。 但是,由于用户可能具有有效的 Windows 标识,因此你仍可能从如下所示的代码中收到 true 值: Thread.CurrentPrincipal.Identity.IsAuthenticated。 若要确定是否为客户端应用程序服务对用户进行身份验证,请确认Identity通过CurrentPrincipalstatic属性检索的 的IPrincipal属性值是引用ClientFormsIdentity。 然后,检查 ClientFormsIdentity.IsAuthenticated 属性。

若要重新对当前用户进行身份验证,请 ClientFormsAuthenticationMembershipProvider.ValidateUser 调用 方法或 staticMembership.ValidateUser 方法。

适用于

另请参阅