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 屬性重設為包含目前 WindowsIdentityWindowsPrincipal 物件。

呼叫此方法之後,目前的使用者就不會再針對用戶端應用程式服務進行驗證。 這表示您無法透過 ClientRoleProvider 類別擷取角色,也無法透過 ClientSettingsProvider 類別來擷取設定。 不過,由於使用者可能具有有效的 Windows 身分識別,您可能仍會收到來自程式代碼的 true 值,例如:Thread.CurrentPrincipal.Identity.IsAuthenticated。 若要判斷使用者是否已針對用戶端應用程式服務進行驗證,請確認透過 staticCurrentPrincipal 屬性擷取之 IPrincipalIdentity 屬性值是 ClientFormsIdentity 參考。 然後,檢查 ClientFormsIdentity.IsAuthenticated 屬性。

若要重新驗證目前的使用者,請呼叫 ClientFormsAuthenticationMembershipProvider.ValidateUser 方法或 staticMembership.ValidateUser 方法。

適用於

另請參閱