2

I have a reporting dll that is used in ASP.NET (Windows Authentication) and winforms projects. Sometimes it needs the username so if it was running in ASP.NET it would use:

System.Web.HttpContext.Current.User.Identity.Name;

and in windows

WindowsIdentity.GetCurrent().Name

Now I am trying to move to .NET 4 Client Profile so I need to avoid the System.Web reference in the reporting dll. Is there an alternative way to get the Windows Username for when the dll is running in ASP.NET which won't use System.Web. The only way I can currently see to avoid System.Web is to pass the Username as a parameter which will be a pain to adjust for in every report.

PeteT
  • 18,754
  • 26
  • 95
  • 132

5 Answers5

5

Maybe you can use the CurrentPrincipal property of the Thread class:

using System.Threading;

string userName = Thread.CurrentPrincipal.Identity.Name;
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Won't this bring up the user under which the site is running on the web server? Where as I need the authenticated end user. – PeteT Jul 19 '11 at 15:36
  • @Pete, since you're using Windows authentication, the thread should be impersonating the authenticated user, so `CurrentPrincipal` should behave as you expect. I did not test it, though (no web server handy right now). – Frédéric Hamidi Jul 19 '11 at 17:46
2

You can use Environment.UserName which lives in System.

Peter Bromberg
  • 1,498
  • 8
  • 11
  • If you're using an AD account to run your IIS site, this will pick up the account running the site, not the user that is visiting the site. Just FYI to others using this – Mattiavelli Dec 22 '15 at 19:09
0

If Windows authentication is on this might work: Page.User.Identity.Name In my case, it does.

tom
  • 15
  • 9
0

System.Web namespace is definitely available in an ASP.Net application. You can definitely make use of it. Apart from that, you will also have the Membership providers that you can use to validate or tell the current user name.

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
  • But you can't add the System.Web reference to an underlying dll you intend to be on the client profile for when in use by WinForms. – PeteT Jul 19 '11 at 14:45
0

Using System.net -- NetworkCredential.Username?

Ref: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.username.aspx

Chains
  • 12,541
  • 8
  • 45
  • 62