Reahreic
Reahreic

Reputation: 629

Retrieve current user's name from active directoy

I'm needing assistance in retrieving the current user's name after they have logged into the site or automatically been given access because they are logged into a computer on the domain with asp.net 4.0 (preferably vb)

I've tried the following calls

HttpContext.Current.Profile.UserName

HttpContext.Current.User.Identity.Name

but they just return blank.

I have no experience with active directory and most of what I've found assumes the program wants to read, write and update the directory service.

Update i've now tried

Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim User As WindowsPrincipal = New WindowsPrincipal(id)
Return User.Identity.Name

and it returns my name as IIS APPPOOL\ASP.NET V4.0

Update 2

Environment.UserName.ToString()

returns ASP.NETv4.0

I've also changed

in the web.config to true where it returns NT AUTHORITY\USR as the current user, which is inline with that i'd expect based on my searches. I'm still unsure as to why when impersonate=false the system returns the asp.net apppool and not the user's name.

Upvotes: 1

Views: 463

Answers (1)

TrevorBrooks
TrevorBrooks

Reputation: 3840

Firstly make sure your web.config is configured as such:

<authentication mode="Windows">

</authentication>

Secondly make sure that your web application in IIS is configured to use Integrated Windows (NTLM) Authentication.

Here is a decent article about this: http://www.asp.net/mvc/tutorials/older-versions/security/authenticating-users-with-windows-authentication-cs

Upvotes: 1

Related Questions