ClientFormsIdentity.RevalidateUser 方法

定义

通过使用缓存凭据来以静默方式验证用户身份。Silently authenticates the user by using cached credentials.

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

示例

下面的示例代码演示如何使用此方法在应用程序离开脱机状态时以无提示方式重新验证用户。The following example code demonstrates how to use this method 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. 如果用户将应用程序设置为 "联机" 状态, 事件处理程序将尝试重新验证用户。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

注解

使用窗体身份验证来验证当前用户时, 只要ClientFormsIdentity应用程序正在运行, 类就会存储用户凭据。When you use forms authentication to validate the current user, the ClientFormsIdentity class stores the user credentials as long as the application is running. 但是, 用户只能在身份验证 cookie 过期之前进行身份验证。However, the user is only authenticated until the authentication cookie expires. Cookie 过期后, 必须重新验证用户以访问远程角色或 Web 设置服务。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, as described in How to: Configure Client Application Services. 但是, 如果将应用程序配置为服从 cookie 过期, 则可以通过调用RevalidateUser方法以编程方式重新验证用户。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.

备注

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.

适用于

另请参阅