共用方式為


HOW TO:使用用戶端應用程式服務實作使用者登入和登出

更新:2007 年 11 月

您可以透過現有的 ASP.NET AJAX 設定檔服務,使用用戶端應用程式服務驗證使用者。如需如何設定 ASP.NET AJAX 設定檔服務的詳細資訊,請參閱搭配 ASP.NET AJAX 使用表單驗證

下列程序描述當應用程式設定為使用其中一個用戶端驗證服務提供者時,如何透過驗證服務驗證使用者。如需詳細資訊,請參閱 HOW TO:設定用戶端應用程式服務

您通常會透過 staticMembership.ValidateUser 方法執行所有驗證。這個方法會透過已設定的驗證提供者管理與驗證服務的互動。如需詳細資訊,請參閱用戶端應用程式服務概觀

表單驗證程序需要存取執行中的 ASP.NET AJAX 驗證服務。如需端對端測試用戶端應用程式服務功能,請參閱逐步解說:使用用戶端應用程式服務

以使用成員資格認證提供者的表單驗證來驗證使用者

  1. 實作 IClientFormsAuthenticationCredentialsProvider 介面。下列程式碼範例示範衍生自 System.Windows.Forms.Form 之對話方塊類別的 IClientFormsAuthenticationCredentialsProvider.GetCredentials 實作。這個對話方塊提供可輸入使用者名稱與密碼的文字方塊,以及「儲存我的資訊」核取方塊。當用戶端驗證提供者呼叫 GetCredentials 方法時,表單便會顯示。使用者在登入對話方塊中填入資訊然後按一下 [確定] 後,就會在新的 ClientFormsAuthenticationCredentials 物件中傳回指定的值。

    Public Function GetCredentials() As  _
        ClientFormsAuthenticationCredentials Implements _
        IClientFormsAuthenticationCredentialsProvider.GetCredentials
    
        If Me.ShowDialog() = DialogResult.OK Then
            Return New ClientFormsAuthenticationCredentials( _
                UsernameTextBox.Text, PasswordTextBox.Text, _
                rememberMeCheckBox.Checked)
        Else
            Return Nothing
        End If
    
    End Function
    
    public ClientFormsAuthenticationCredentials GetCredentials()
    {
        if (this.ShowDialog() == DialogResult.OK)
        {
            return new ClientFormsAuthenticationCredentials(
                usernameTextBox.Text, passwordTextBox.Text,
                rememberMeCheckBox.Checked);
        }
        else
        {
            return null;
        }
    }
    
  2. 呼叫 staticMembership.ValidateUser 方法,然後以空字串當做參數值傳入。當您指定空字串時,這個方法會針對為應用程式所設定之認證提供者,於內部呼叫 GetCredentials 方法。下列程式碼範例會呼叫這個方法,限制存取整個 Windows Form 應用程式。您可以將這段程式碼加入至 Form.Load 處理常式。

    If Not System.Web.Security.Membership.ValidateUser( _
        String.Empty, String.Empty) Then
    
        MessageBox.Show("Unable to authenticate.", "Not logged in", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Application.Exit()
    
    End If
    
    if (!System.Web.Security.Membership.ValidateUser(
        String.Empty, String.Empty))
    {
        MessageBox.Show("Unable to authenticate.", "Not logged in",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        Application.Exit();
    }
    

以不使用成員資格認證提供者的表單驗證來驗證使用者

  • 呼叫 staticMembership.ValidateUser 方法,然後傳入從使用者處擷取的使用者名稱與密碼值。

    If Not System.Web.Security.Membership.ValidateUser( _
        usernameTextBox.Text, passwordTextBox.Text) Then
    
        MessageBox.Show("Unable to authenticate.", "Not logged in", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        Application.Exit()
    
    End If
    
    if (!System.Web.Security.Membership.ValidateUser(
        usernameTextBox.Text, passwordTextBox.Text))
    {
        MessageBox.Show("Unable to authenticate.", "Not logged in",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        Application.Exit();
    }
    

以 Windows 驗證來驗證使用者

  • 呼叫 staticMembership.ValidateUser 方法,然後傳遞空字串的參數。這個方法呼叫一定會傳回 true,並且將 Cookie 加入至其中包含 Windows 識別之使用者的 Cookie 快取中。

    System.Web.Security.Membership.ValidateUser( _
        String.Empty, String.Empty)
    
    System.Web.Security.Membership.ValidateUser(
        String.Empty, String.Empty);
    

穩固程式設計

這個主題中的範例程式碼會示範 Windows 用戶端應用程式中驗證的最簡單用法。當您使用用戶端應用程式服務與表單驗證呼叫 staticMembership.ValidateUser 方法時,您的程式碼可能會擲回 WebException。這表示驗證服務無法使用。如需如何處理這個例外狀況的範例,請參閱逐步解說:使用用戶端應用程式服務

請參閱

工作

HOW TO:設定用戶端應用程式服務

逐步解說:使用用戶端應用程式服務

概念

用戶端應用程式服務概觀

搭配 ASP.NET AJAX 使用表單驗證

其他資源

用戶端應用程式服務