I have a custom HTTP Module running on IIS 7.5, and the requests will be coming from different machines in the same Active Directory environment. Is it possible for me to get the current user (their AD Name?) from a request (or look up, if I knew the LDAP path?) in an HTTP Module running server side?
I'm not AD expert, but I've looked through the different properties that come through the HTTPApplication
instance (see below) within my module, and I'm not seeing anything obvious.
Private Sub AuthenticateRequest(sender As Object, e As EventArgs)
Dim oHttpApplication As HttpApplication = CType(sender, HttpApplication)
'...
' Get AD Info from oHttpApplication.Request?
'...
End Sub
Does anyone know if this is possible or know of a way to accomplish this?
UPDATE:
I've added my solution below.
My solution - in VB.NET:
Dim username As String = Thread.CurrentPrincipal.Identity.Name
NOTE: The answer to the question in my link goes on to use a PrincipalContext
and UserPrincipal
object, but for my scenario all I needed was the username. I would also like to point out that when testing that solution, this line:
pc = new PrincipalContext(ContextType.Domain, "active.directory.domain.com")
Also works fine without the domain for me:
pc = new PrincipalContext(ContextType.Domain)
So hopefully this will be helpful to someone in the future!