ClientFormsIdentity 類別

定義

代表已使用表單驗證,經用戶端應用程式服務驗證的使用者識別。

public ref class ClientFormsIdentity : IDisposable, System::Security::Principal::IIdentity
public class ClientFormsIdentity : IDisposable, System.Security.Principal.IIdentity
type ClientFormsIdentity = class
    interface IIdentity
    interface IDisposable
Public Class ClientFormsIdentity
Implements IDisposable, IIdentity
繼承
ClientFormsIdentity
實作

範例

下列範例程式碼示範如何在應用程式離開離線狀態時,使用此類別以無訊息方式重新驗證使用者。 在此範例中 CheckedChanged ,事件處理常式會更新離線狀態,以符合核取方塊值。 如果使用者將應用程式設定為線上狀態,事件處理常式會呼叫 方法來嘗試重新驗證使用者 RevalidateUser 。 不過,如果驗證服務器無法使用,事件處理常式會將應用程式傳回離線狀態。

注意

RevalidateUser 方法只是為了方便而使用。 因為它沒有傳回值,所以無法表示重新驗證是否失敗。 例如,如果在伺服器上的使用者認證已變更,重新驗證就可能會失敗。 在這種情況下,您可能想要加入在服務呼叫失敗之後能夠明確驗證使用者的程式碼。 For more information, see the Accessing Web Settings section in Walkthrough: Using Client Application Services.

private void workOfflineCheckBox_CheckedChanged(
    object sender, EventArgs e)
{
    ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked;
    if (!ConnectivityStatus.IsOffline)
    {
        try
        {
            // Silently re-validate the user.
            ((ClientFormsIdentity)
                System.Threading.Thread.CurrentPrincipal.Identity)
                .RevalidateUser();

            // If any settings have been changed locally, save the new
            // new values to the Web settings service.
            SaveSettings();

            // If any settings have not been changed locally, check 
            // the Web settings service for updates. 
            Properties.Settings.Default.Reload();
        }
        catch (System.Net.WebException)
        {
            MessageBox.Show(
                "Unable to access the authentication service. " +
                Environment.NewLine + "Staying in offline mode.",
                "Warning", MessageBoxButtons.OK, 
                MessageBoxIcon.Warning);
            workOfflineCheckBox.Checked = true;
        }
    }
}
Private Sub workOfflineCheckBox_CheckedChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles workOfflineCheckBox.CheckedChanged

    ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked
    If Not ConnectivityStatus.IsOffline Then

        Try

            ' Silently re-validate the user.
            CType(System.Threading.Thread.CurrentPrincipal.Identity,  _
                ClientFormsIdentity).RevalidateUser()

            ' If any settings have been changed locally, save the new
            ' new values to the Web settings service.
            SaveSettings()

            ' If any settings have not been changed locally, check 
            ' the Web settings service for updates. 
            My.Settings.Reload()

        Catch ex As System.Net.WebException

            MessageBox.Show( _
                "Unable to access the authentication service. " & _
                Environment.NewLine + "Staying in offline mode.", _
                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            workOfflineCheckBox.Checked = True

        End Try

    End If
End Sub

備註

用戶端應用程式服務功能會使用此類別來代表已驗證的使用者。 當您將應用程式設定為使用用戶端應用程式服務和表單驗證時,您可以呼叫 staticMembership.ValidateUser 方法來驗證使用者。 驗證之後,您可以透過透過 Identity 屬性擷取的 IPrincipal 屬性,擷取目前 ClientFormsIdentity 實例的 staticThread.CurrentPrincipal 參考。 如需詳細資訊,請參閱用戶端應用程式服務

您通常會存取 ClientFormsIdentity 物件做為 IIdentity 參考,以避免直接相依于這個類別。 您可以藉由檢查 IIdentity.IsAuthenticated 身分識別的 屬性來判斷使用者是否經過驗證。 不過,使用者可能會針對 Windows 進行驗證,但不適用於用戶端應用程式服務。 若要判斷使用者是否已針對用戶端應用程式服務進行驗證,您也應該確認 IIdentity.AuthenticationType 屬性值為 「ClientForms」。

您必須使用明確的 ClientFormsIdentity 參考來呼叫 RevalidateUser 方法,該方法不是由 介面所 IIdentity 定義。

建構函式

ClientFormsIdentity(String, String, MembershipProvider, String, Boolean, CookieContainer)

初始化 ClientFormsIdentity 類別的新執行個體。

屬性

AuthenticationCookies

取得從驗證服務所擷取的 Cookie 的集合。

AuthenticationType

取得用於驗證使用者的驗證類型。

IsAuthenticated

取得值,指出使用者是否已經過驗證。

Name

取得使用者的名稱。

Provider

取得用於驗證使用者的成員資格提供者。

方法

Dispose()

釋放 ClientFormsIdentity 所使用的所有資源。

Dispose(Boolean)

釋放 ClientFormsIdentity 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RevalidateUser()

使用快取的認證,以無訊息模式驗證使用者。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱