Active Directory Authentication means user will be authenticate with their User ID and Password in Active directory.
User Id belongs to your Window id and password belongs to your window password.
Login.aspx Page
<table width="100%" class="loginblock" style="text-align: center; vertical-align: middle;"> <tr> <td colspan="2" height="20px"> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td style="color: #011273; font-family: Verdana; width: 290px" align="right" height="30px"> <b>User Id</b> </td> <td align="left" height="30px"> <asp:TextBox ID="txtUserId" runat="server" Width="110px" Style="text-transform: uppercase"></asp:TextBox></td> </tr> <tr> <td style="color: #011273; font-family: Verdana;" align="right" height="30px"> <b>Password</b> </td> <td align="left" height="30px"> <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="110px"></asp:TextBox></td> </tr> <tr> <td></td> <td align="left" style="padding-left: 14px"> <asp:Button ID="lnkEnterSite" runat="server" OnClick="lnkEnterSite_Click" Text="ENTER" ValidationGroup="Login" OnClientClick="return StartProgressBar();" /></td> </tr> <tr> <td colspan="2"> <asp:HiddenField ID="hdnLoginRequestId" runat="server" /> </td> </tr> </table>
On Enter button click event write the following code
protected void lnkEnterSite_Click(object sender, EventArgs e) { try { string sDomain = “Your Domain Name” string adPath = "LDAP://" + sDomain; LdapAuthentication adAuth = new LdapAuthentication(adPath); if(true == adAuth.IsAuthenticated(sDomain, txtUserId.Text.Trim(), txtPwd.Text.Trim())) { if(Authenticate(txtUserId.Text.Trim())) { Response.Redirect(Your Default Page); } else { ErrorMsg = "You do not have access to this site” } } else { ErrorMsg = "Authentication failed. Check user name and password.”; } } }
Check user exists in Active Directory
public bool IsAuthenticated(string sDomain, string username, string pwd) { string domainAndUsername = sDomain + @"\" + username; DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd, AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind); try { DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne(); if (null == result) { return false; } //Update the new path to the user in the directory. _path = result.Path; _filterAttribute = (string)result.Properties["cn"][0]; } catch (Exception ex) { throw new Exception("Error authenticating user. " + ex.Message); } return true; }
Check user exists in your database
protected bool Authenticate(String userName) { If exists Return true Else Return false }
Thanks for your collaboration.