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.CurrentPrincipal 并将 属性重置为 WindowsPrincipal 包含当前 WindowsIdentity的对象。

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

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

适用于

另请参阅