All,
I have an windows application which can be used in organization level. So we would like to put windows authentication. So I searched and used below code. How to verify the user based on the User name and domain. In the below code, we are passing static LogonType and LogonProvider. So I would like to validate the username and domain against the active directory. For the below code I did not specify the Active Directory path, but i dont know how its working.
Please give a solution to verify username and domain against Active Directory in the server.
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
public bool IsValidateCredentials(string userName, string password, string domain)
{
IntPtr tokenHandler = IntPtr.Zero;
bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
return isValid;
}
private void button1_Click_1(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
bool isValid = IsValidateCredentials(txtUserName.Text, txtPassword.Text, txtDomain.Text);
if (isValid)
{
RadForm1 form = new RadForm1();
form.Show();
this.Hide();
}
else
{
lblMsg2.Text = isValid == true ? "Valid User details" : "Invalid User Details";
}
}
Please let me know if I am not clear...
Thanks,
Naveen