TheDudeAbides
TheDudeAbides

Reputation: 131

Get a visitors AD user name in c#

If I have a basic web form for our company's intranet and all users are logged in to the domain via AD authentication, can I extract a visitors user name in c#?

Upvotes: 2

Views: 805

Answers (2)

to StackOverflow
to StackOverflow

Reputation: 124716

If you are using Windows Authentication, and don't allow anonymous access, then HttpContext.Current.User.Identity.Name will have the name of the currently logged on user in the format domain\username.

If you want more info from AD (e.g. a display name), then you need to use the classes in the System.DirectoryServices namespace. You may need to provide credentials to access AD if your site is running under a restricted account such as the Network Service account.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532475

Use the User property on the current Page, like so,

var username = this.User.Identity.Name

Upvotes: 1

Related Questions