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 方法只是為了方便而使用。 因為它沒有傳回值,所以無法表示重新驗證是否失敗。 例如,如果在伺服器上的使用者認證已變更,重新驗證就可能會失敗。 在這種情況下,您可能想要加入在服務呼叫失敗之後能夠明確驗證使用者的程式碼。 For more information, see the Accessing Web Settings section in Walkthrough: Using Client Application Services.

適用於

另請參閱