aninec
asked on
ASP.NET w/ VB Getting UserName after isAuthenticated from Active Directory
I have three issues that I am struggling with in my code for authenticating users in AD.
1. I want to pass the Username to the next page so that I can use it in an Audit Trail later on during the users experience with the app. I do not want to use a query string.
2. I want to get the users First and Last Name after authenticating. I have no clue how to do this and no clear explanation on the web has been found.
3. I want the user to be a member a specific group in AD. I am using LDAP
Can anyone help me thorugh these issues? I have included my code below - works fine except that any member of the domain can login..not just members of the group
I am assuming that the majority of the code needs to go here except for the username,etc that will passed off to the next page.
I have this for getting at the specific group but I can not figure out how to use this in my code. As is - does not work.
LDAP://my.domain.org/CN=OU =ABC Managed Users,OU=Users,OU=MySite,O U=ABC, DC=my,DC=domain,DC=org
1. I want to pass the Username to the next page so that I can use it in an Audit Trail later on during the users experience with the app. I do not want to use a query string.
2. I want to get the users First and Last Name after authenticating. I have no clue how to do this and no clear explanation on the web has been found.
3. I want the user to be a member a specific group in AD. I am using LDAP
Can anyone help me thorugh these issues? I have included my code below - works fine except that any member of the domain can login..not just members of the group
I am assuming that the majority of the code needs to go here except for the username,etc that will passed off to the next page.
I have this for getting at the specific group but I can not figure out how to use this in my code. As is - does not work.
LDAP://my.domain.org/CN=OU
imports System
imports System.Web.Security
imports System.Security.Principal
imports System.Web
imports System.DirectoryServices
imports System.DirectoryServices.DirectoryEntry
Partial Class Authenticate_With_Active_Directory
Inherits System.Web.UI.Page
Protected Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim isAuthenticated As Boolean = AuthenticateUser()
If isAuthenticated Then
Dim sScript As String
Dim cScript As String
sScript = "<SCRIPT Language=JavaScript> newWin = window.location.href='/default.aspx'; </SCRIPT>"
RegisterStartupScript("NewWindow", sScript)
cScript = "<script language='javascript'> { window.close() }</script>"
Else
Incorrect.visible = True
End If
End Sub
Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean
Dim Success As Boolean = False
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)
Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
Try
Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne
Success = Not (Results Is Nothing)
Dim aCookie As New HttpCookie("lastVisit")
aCookie.Values("userName") = txtUserName.Text
aCookie.Values("lastVisit") = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddHours(4)
Response.Cookies.Add(aCookie)
Catch
Success = False
End Try
Return Success
End Function
Private Function AuthenticateUser() As Boolean
Dim username As String = txtUsername.Text
Dim password As String = txtPassword.Text
Dim domain As String = "my.domain.org"
Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password)
Return isAuthenticated
End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PrincipalContext pContext = new PrincipalContext(ContextTy
Principal p = Principal.FindByIdentity(p
//Got an instance to a managed reference of the AD Account
DirectoryEntry dirEntry = (DirectoryEntry)p.GetUnder
string FirstName = dirEntry.Properties["First
string LastName = dirEntry.Properties["LastN