1

This question is solved with Framework 3.5, however I need to do this using .NET Framework 2 and C#.

I want to validate a given domain\user and password combination. For example:

Username: TheDomain\TheName
Password: ThePassword

Also note the drawbacks to the suggested solutions given in the link.

Current solution that I am using, however note the false negative possibilities

Community
  • 1
  • 1
GKruger
  • 137
  • 1
  • 8

1 Answers1

1

You can use NetworkCredential.Domain Property

The following code example uses the Domain property to set the domain associated with the credentials

// 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();

Here is the MSDN Link for more reading

Hope it will help

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30