Επεξεργασία

Κοινή χρήση μέσω


ClientFormsIdentity Class

Definition

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
Inheritance
ClientFormsIdentity
Implements

Examples

The following example code demonstrates how to use this class to silently revalidate a user when the application leaves the offline state. In this example, a CheckedChanged event handler updates the offline status to match the check box value. 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.

Note

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

Remarks

The client application services feature uses this class to represent an authenticated user. 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. 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.

You will typically access a ClientFormsIdentity object as an IIdentity reference to avoid a direct dependency on this class. You can determine whether a user is authenticated by checking the IIdentity.IsAuthenticated property of the identity. However, the user may be authenticated for Windows, but not for client application services. To determine whether the user is authenticated for client application services, you should also confirm that the IIdentity.AuthenticationType property value is "ClientForms".

You must use an explicit ClientFormsIdentity reference to call the RevalidateUser method, which is not defined by the IIdentity interface.

Constructors

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

Initializes a new instance of the ClientFormsIdentity class.

Properties

AuthenticationCookies

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.

Methods

Dispose()

Releases all resources used by the ClientFormsIdentity.

Dispose(Boolean)

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.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RevalidateUser()

Silently authenticates the user by using cached credentials.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also