ClientFormsIdentity 類別

定義

代表已使用表單驗證,經用戶端應用程式服務驗證的使用者識別。Represents a user identity authenticated for client application services by using forms authentication.

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
實作

範例

下列範例程式碼示範如何使用這個類別, 在應用程式離開離線狀態時, 以無訊息方式重新驗證使用者。The following example code demonstrates how to use this class to silently revalidate a user when the application leaves the offline state. 在此範例中, CheckedChanged事件處理常式會更新離線狀態, 以符合核取方塊的值。In this example, a CheckedChanged event handler updates the offline status to match the check box value. 如果使用者將應用程式設定為線上狀態, 事件處理常式會藉由呼叫RevalidateUser方法, 嘗試重新驗證使用者。If the user sets the application to the online state, the event handler attempts to revalidate the user by calling the RevalidateUser method. 不過, 如果驗證服務器無法使用, 則事件處理常式會將應用程式傳回離線狀態。However, if the authentication server is unavailable, the event handler returns the application to the offline state.

注意

RevalidateUser 方法只是為了方便而使用。The RevalidateUser method is for convenience only. 因為它沒有傳回值,所以無法表示重新驗證是否失敗。Because it does not have a return value, it cannot indicate whether revalidation has failed. 例如,如果在伺服器上的使用者認證已變更,重新驗證就可能會失敗。Revalidation can fail, for example, if the user credentials have changed on the server. 在這種情況下,您可能想要加入在服務呼叫失敗之後能夠明確驗證使用者的程式碼。In this case, you might want to include code that explicitly validates users after a service call fails. 如需詳細資訊, 請參閱逐步解說:使用用戶端應用程式服務。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

備註

用戶端應用程式服務功能會使用此類別來代表已驗證的使用者。The client application services feature uses this class to represent an authenticated user. 當您將應用程式設定為使用用戶端應用程式服務與表單驗證時, 您可以藉static由呼叫Membership.ValidateUser方法來驗證使用者。When you configure your application to use client application services and forms authentication, you can authenticate a user by calling the static Membership.ValidateUser method. 驗證之後, ClientFormsIdentity您可以Identity透過透過static Thread.CurrentPrincipal屬性抓取之的IPrincipal屬性, 抓取目前實例的參考。After authentication, you can retrieve a reference to the current ClientFormsIdentity instance through the Identity property of the IPrincipal retrieved through the static Thread.CurrentPrincipal property. 如需詳細資訊,請參閱用戶端應用程式服務For more information, see Client Application Services.

您通常會存取ClientFormsIdentity物件IIdentity做為參考, 以避免直接相依于此類別。You will typically access a ClientFormsIdentity object as an IIdentity reference to avoid a direct dependency on this class. 您可以藉由檢查IIdentity.IsAuthenticated身分識別的屬性, 來判斷使用者是否通過驗證。You can determine whether a user is authenticated by checking the IIdentity.IsAuthenticated property of the identity. 不過, 使用者可能已針對 Windows 進行驗證, 但不適用於用戶端應用程式服務。However, the user may be authenticated for Windows, but not for client application services. 若要判斷使用者是否已針對用戶端應用程式服務進行驗證, 您也應該IIdentity.AuthenticationType確認屬性值為 "ClientForms"。To determine whether the user is authenticated for client application services, you should also confirm that the IIdentity.AuthenticationType property value is "ClientForms".

您必須使用明確ClientFormsIdentity的參考來RevalidateUser呼叫方法, 而這不IIdentity是由介面所定義。You must use an explicit ClientFormsIdentity reference to call the RevalidateUser method, which is not defined by the IIdentity interface.

建構函式

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

初始化 ClientFormsIdentity 類別的新執行個體。Initializes a new instance of the ClientFormsIdentity class.

屬性

AuthenticationCookies

取得從驗證服務所擷取的 Cookie 的集合。Gets a collection of the cookies retrieved from the authentication service.

AuthenticationType

取得用於驗證使用者的驗證類型。Gets the type of authentication that is used to authenticate the user.

IsAuthenticated

取得值,指出使用者是否已經過驗證。Gets a value indicating whether the user has been authenticated.

Name

取得使用者的名稱。Gets the name of the user.

Provider

取得用於驗證使用者的成員資格提供者。Gets the membership provider that is used to authenticate the user.

方法

Dispose()

釋放 ClientFormsIdentity 所使用的所有資源。Releases all resources used by the ClientFormsIdentity.

Dispose(Boolean)

釋放 ClientFormsIdentity 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the ClientFormsIdentity and optionally releases the managed resources.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
RevalidateUser()

使用快取的認證,以無訊息模式驗證使用者。Silently authenticates the user by using cached credentials.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱