Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
       public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);


C#
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;
       }


C#
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
Posted

This is the code I use verify a user has an Active Direct login.
VB
Private Function ValidateActiveDirectoryLogin(ByVal Domain As String,
                                              ByVal Username As String,
                                              ByVal Password As String) As Boolean
   ' Connect to the domain.
   Using oDomainInformation As New PrincipalContext(ContextType.Domain, Domain,
                                                    ContextOptions.Negotiate)
        ' Validate the credentials.
        Return oDomainInformation.ValidateCredentials(Username, Password)
    End Using
End Function
 
Share this answer
 
You should be able to use the standard Windows membership and role provider for this application.

In Visual Studio select your Project and you should have a Properties display under your Solution Explorer. Disable Anonymous Authentication and enable Windows Authentication.

This will modify your web.config to use the domain that the webserver is a member of.

All this really does is add the following to your <system.web> entries:

XML
<authentication mode="Windows" />
<authorization>
  <deny users="?" />
</authorization>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900