ClientFormsIdentity.RevalidateUser 方法

定义

通过使用缓存凭据来以静默方式验证用户身份。

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

示例

以下示例代码演示如何在应用程序离开脱机状态时使用此方法以无提示方式重新验证用户。 在此示例中,CheckedChanged事件处理程序更新脱机状态以匹配检查框值。 如果用户将应用程序设置为联机状态,事件处理程序将尝试重新验证用户。 但是,如果身份验证服务器不可用,事件处理程序会将应用程序返回到脱机状态。

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

注解

使用 Forms 身份验证验证当前用户时, ClientFormsIdentity 类会存储用户凭据,前提是应用程序正在运行。 但是,只有在身份验证 Cookie 过期之前,才会对用户进行身份验证。 Cookie 过期后,必须重新验证用户才能访问远程角色或 Web 设置服务。 可以使用 “ 服务的高级设置 ” 对话框将应用程序配置为自动重新验证用户。 但是,如果将应用程序配置为遵循 Cookie 过期,则可以通过调用 RevalidateUser 方法以编程方式重新验证用户。 从脱机模式切换到联机模式时,此方法也很有用,因为应用程序可能在脱机时已关闭。

注意

RevalidateUser 方法只是为了方便。 因为它没有返回值,所以无法指示重新验证是否失败。 例如,如果在服务器上更改了用户凭据,则重新验证可能会失败。 在这种情况下,你可能要包含在服务调用失败之后显式验证用户的代码。 有关详细信息,请参阅 演练:使用客户端应用程序服务中的访问 Web 设置部分。

适用于

另请参阅