VB.NET • Authenticate and Initialize

Listing 2. The Login routine authenticates the ADAM user, extracts the SID, and uses it to initialize the Authorization Manager client context.

Public Shared Function Login(ByVal p_Name As String, ByVal _
     p_PWD As String) As AdamPrincipal
     Try
          Dim l_entry As New DirectoryEntry( _
               "LDAP://" + "localhost" + ":" + _
               "1389" + "/" + "DC=MyApp,DC=SABBASOFT,DC=COM", _
               pf_makeFullDN(p_Name), p_PWD, _
               AuthenticationTypes.None)
          Dim mySearcher As New DirectorySearcher(l_entry)
          mySearcher.Filter = ("(&(objectClass=user)( _
               name=" + p_Name + "))")
          Dim l_s As SearchResult = mySearcher.FindOne
          If l_s Is Nothing Then Throw New Exception( _
               "Critical Error, Couldn't find logged user")
          Dim SID As Byte() = _
               l_s.Properties.Item("objectSID").Item(0)
          Dim sSID As String
          Dim sidPtr As IntPtr = Marshal.AllocHGlobal(SID.Length)
          Marshal.Copy(SID, 0, sidPtr, SID.Length)
          ConvertSidToStringSid(CType(sidPtr, IntPtr), sSID)
          Dim l_IAzClientContext As IAzClientContext = _ 
               m_app.InitializeClientContextFromStringSid(sSID, _ 
               tagAZ_PROP_CONSTANTS.AZ_CLIENT_CONTEXT_SKIP_GROUP)
     Catch ex As Exception
          Throw (New Exception("Login Failed"))
     End Try
End Function