ASP.NET(vb.net) & User Authentication Active Directory (AD) - This is article and example scripts how to use LoginĀ Active Directory (AD) using ASP.NET scripts.
ShotDev Focus:
- ASP.NET(vb.net) & User Authentication Active Directory (AD)
Example
AspNetActiveDirectory.aspx
<%@ Assembly Name="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%> <%@ Import Namespace="System.DirectoryServices"%> <%@ Import Namespace="System.Security.Cryptography"%> <%@ Import Namespace="System.Text"%> <%@ Page Language="VB" %> <script runat="server"> Dim initLDAPPath = "dc=ad, dc=shotdev, dc=com" Dim initLDAPServer = "192.168.0.1" Dim initShortDomainName = "ad" Dim strErrMsg Public Function MD5(ByVal strString As String) As String Dim ASCIIenc As New ASCIIEncoding Dim strReturn As String Dim ByteSourceText() As Byte = ASCIIenc.GetBytes(strString) Dim Md5Hash As New MD5CryptoServiceProvider Dim ByteHash() As Byte = Md5Hash.ComputeHash(ByteSourceText) strReturn = "" For Each b As Byte In ByteHash strReturn = strReturn & b.ToString("x2") Next Return strReturn End Function Sub Page_Load(sender As Object, e As EventArgs) End Sub Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim DomainAndUsername As String = "" Dim strCommu As String Dim flgLogin As Boolean = False strCommu = "LDAP://" & initLDAPServer & "/" & initLDAPPath DomainAndUsername = initShortDomainName & "\" & txtUser.Text Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text) Dim obj As Object Try obj = entry.NativeObject Dim search As New DirectorySearcher(entry) Dim result As SearchResult search.Filter = "(SAMAccountName=" + txtUser.Text + ")" search.PropertiesToLoad.Add("cn") result = search.FindOne() If result Is Nothing Then flgLogin = False strErrMsg = "Please check user/password" Else flgLogin = True End If Catch ex As Exception flgLogin = False strErrMsg = "Please check user/password" End Try IF flgLogin = True Me.lbDisplay.Text = "Welcome " & txtUser.Text Else Me.lbDisplay.Text = strErrMsg End IF End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:Label ID="lblUser" Text="Username" runat="server" /> <br /> <asp:TextBox ID="txtUser" runat="server" Width="147px"></asp:TextBox> <br /> <asp:Label ID="lblPassword" Text="Password" runat="server" /> <br /> <asp:TextBox ID="txtPwd" runat="server" Width="147px" TextMode="Password"></asp:TextBox> <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" /> <br /> <asp:Label ID="lbDisplay" runat="server"></asp:Label> </form> </body> </html>
Screenshot