Hello Christian,
Thank you very much for your reply.
I've solved the issue myself.
I am posting the code below, which may help others:
Step 1:
Create new project and Create New Webservice.
Step 2:
Write the following method:
[WebMethod(Description = "Validate a username and password against Active Directory")]
public bool Authentication(string domain, string username, string password)
{
var val = true;
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, password);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
var uname = search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
{
val = false;
}
}
catch
{
val = false;
}
return val;
}
Step 3:
Consume this web service in your project.
private void Register_Click(object sender, RoutedEventArgs e)
{
try
{
if (txtUsername.Text.Trim() == "")
{
lblStatus.Text = "Please Enter Username";
}
else if (txtPassword.Password == "")
{
lblStatus.Text = "Please Enter Password";
}
else
{
WS.Domain_AuthSoapClient client = new WS.Domain_AuthSoapClient();
client.AuthenticationCompleted += new EventHandler<authenticationcompletedeventargs>(Client_AuthenticationCompleted);
client.AuthenticationAsync(" your domain name", txtUsername.Text, txtPassword.Password);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void Client_AuthenticationCompleted(object sender, AuthenticationCompletedEventArgs e)
{
if (e.Error != null)
{
lblStatus.Text = "Sorry, an error occured. " + e.Error.Message;
}
if (e.Result == true)
{
}
else
{
}
}
</authenticationcompletedeventargs>
Thank you very much.