4

We have an ASP.NET intranet site, available to users who are either physically at and logged into a local machine or else to users who connect remotely via VPN. Is there a way to automatically get the username of whoever is accessing the page based on the name used to either log into the local machine or that used for the VPN?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
notnot
  • 4,472
  • 12
  • 46
  • 57

5 Answers5

13

This is possible if you're using Windows Authentication in your pages.

You can use either Page.User.Identity.Name or the more complete System.Web.HttpContext.Current.User.Identity.Name.

Read more about it here:

Enabling Windows Authentication within an Intranet ASP.NET Web Application

If you are, however, using Forms Authentication, you'll have to keep track of the current user yourself, the most common method of which will be by storing their login name in a Session variable.

Jon Limjap
  • 94,284
  • 15
  • 101
  • 152
  • Just a note but if you use forms authentication `User.Identity.Name` will also contains the forms auth username as well. – Brent Pabst Aug 07 '12 at 16:20
3

Get the User information as follows:

User.Identity.Name \\ Get the User name
User.Identity.AuthenticationType \\ What is the Authentication type
User.Identity.IsAuthenticated \\ Is he authenticated?
User.IsInRole("Administrators") \\ Is he administrator?
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Kisan Patel
  • 101
  • 3
3

If the authentication is setup to do Integrated Windows authentication then you should be able to get it by accessing

User.Identity.Name
David
  • 19,389
  • 12
  • 63
  • 87
1

If the authentication is windows, this should help:

IIdentity WinId= HttpContext.Current.User.Identity;
WindowsIdentity wi = (WindowsIdentity)WinId;
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
1

You can use

Page.User.Identity.Name
cjk
  • 45,739
  • 9
  • 81
  • 112