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,並將 屬性 WindowsPrincipal 重設為 staticThread.CurrentPrincipal 包含目前 WindowsIdentity 的物件。

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

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

適用於

另請參閱