2013-07-19 85 views

回答

1

您可以使用NetworkCredential.Domain物業

下面的代碼示例使用域名屬性來設置與該憑證關聯的域

// Create an empty instance of the NetworkCredential class. 
NetworkCredential myCredentials = new NetworkCredential("", "", ""); 
myCredentials.Domain = domain; 
myCredentials.UserName = username; 
myCredentials.Password = password; 

// Create a WebRequest with the specified URL. 
WebRequest myWebRequest = WebRequest.Create(url); 
myWebRequest.Credentials = myCredentials; 
Console.WriteLine("\n\nUser Credentials:- Domain: {0} , UserName: {1} , Password: {2}", 
        myCredentials.Domain, myCredentials.UserName, myCredentials.Password); 

// Send the request and wait for a response. 
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...Please wait ..."); 
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Process the response. 
Console.WriteLine("\nResponse received sucessfully"); 

// Release the resources of the response object. 
myWebResponse.Close(); 

這裏是MSDN Link更多閱讀

希望它會幫助

+0

我們是否需要在計算機上運行IIS,才能使用這段代碼? –

+0

Domain屬性用於與Active Directory域進行NTLM身份驗證 – Microtechie