2012-08-09 148 views
2

我有一個網站:「https://blahblah.com」驗證網站的憑證

爲了驗證它,我這樣做(工作正常):

NetworkCredential credentials = new NetworkCredential(); 
      credentials.UserName = AppVars.Username; 
      credentials.Password = AppVars.Password; 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      request.Credentials = credentials; 
      //..... 

但我怎麼如果我想要添加登錄功能,只需驗證用戶名和密碼?

更新的代碼:

private void btnLogIn_Click(object sender, EventArgs e) 
     { 
      Properties.Settings.Default.Username = txtUserName.Text; 
      Properties.Settings.Default.Password = txtPassword.Text; 

      using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, AppVars.ixLibraryConnectionTestURL)) 
      { 
       try 
       { 
        bool isValid = false; 
        isValid = pc.ValidateCredentials(AppVars.Username, AppVars.Password); 
        if (isValid == true) 
        { 
         //just testing 
         MessageBox.Show("is valid"); 
        } 
        else 
        { 
         //just testing 
         MessageBox.Show("is not valid"); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      }  
     } 

域名看起來是這樣的:https://xxxxxx-services.zzz999.org/pqg_4/lib/api/sdo/rest/v1

+0

你在.NET 3.5中使用C#嗎? – KKP 2012-08-09 14:27:39

+1

正確。即時通訊在.NET 3.5中使用C#。 – Testifier 2012-08-09 14:31:04

回答

1

用途:System.DirectoryServices.AccountManagement命名空間

// create a "principal context" - e.g. your domain (could be machine, too) 

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")) 
{  

// validate the credentials  
bool isValid = pc.ValidateCredentials("myuser", "mypassword"); 
} 

你可以閱讀更多關於它在這裏:

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx

+0

我怎麼不能添加這個命名空間?即時通訊使用.net 3.5 btw。當我鍵入使用System.Direc(它沒有找到directoryServices)... – Testifier 2012-08-09 14:35:53

+0

沒關係,我添加了參考。讓我們現在就來試試這個代碼。 – Testifier 2012-08-09 14:37:52

+0

好的,讓我知道它是怎麼回事。 – KKP 2012-08-09 14:43:32