GKruger
GKruger

Reputation: 137

Validate domain user credentials with .NET 2

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

Upvotes: 1

Views: 3208

Answers (1)

Rajeev Bera
Rajeev Bera

Reputation: 2019

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

Upvotes: 1

Related Questions