Rediger

Del via


ClientFormsIdentity.RevalidateUser Method

Definition

Silently authenticates the user by using cached credentials.

public:
 void RevalidateUser();
public void RevalidateUser ();
member this.RevalidateUser : unit -> unit
Public Sub RevalidateUser ()

Examples

The following example code demonstrates how to use this method 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. However, if the authentication server is unavailable, the event handler returns the application to the offline state.

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

When you use forms authentication to validate the current user, the ClientFormsIdentity class stores the user credentials as long as the application is running. However, the user is only authenticated until the authentication cookie expires. After the cookie expires, the user must be revalidated to access the remote roles or Web settings services. You can use the Advanced Settings for Services dialog box to configure your application to automatically revalidate the user. However, if you configure your application to honor the cookie expiration, you can programmatically revalidate the user by calling the RevalidateUser method. This method is also useful when switching from offline mode to online mode, because the application may have been shut down while offline.

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.

Applies to

See also