使用.NET 2验证域用户凭据

时间:2013-07-19 09:05:44

标签: c# windows authentication

使用Framework 3.5解决了

This question,但我需要使用.NET Framework 2和C#来完成此任务。

我想验证给定的域\用户和密码组合。例如:

Username: TheDomain\TheName
Password: ThePassword

还要注意链接中给出的建议解决方案的缺点。

然而,

Current solution that I am using注意到错误的消极可能性

1 个答案:

答案 0 :(得分:1)

您可以使用NetworkCredential.Domain属性

以下代码示例使用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以获取更多阅读

希望它会有所帮助