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

注解

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

注释

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

适用于

另请参阅