How to get the UserName for both web and client apps?

D

davshu

I need to get the username (domain\user) in a way that will work for both web
and client apps.

For web I would just use HttpContext.Current.User.Identity.Name
But this won't work for a client app.

I tried System.Security.Principal.WindowsIdentity.Name but my web app
returned a system account, not the authenticated user.

Anyone have any ideas?
 
C

Cor Ligthert[MVP]

What do you mean with a client app.

If that is a windows form then it is Environment.UserName;

Cor
 
M

Marc Gravell

There is an abstraction you can use here; the principal / identity;
you should be able to look at Thread.CurrentPrincipal.Identity.Name

Advantages:
* It will work with the ASP.NET provider
* It will work with WCF (HttpContext won't)
* It will work with winform (assuming your winform sets the principal
at start)
* You can use it anywhere without needing to know the architecture
* You can use it to enforce code access - i.e. [PrincipalPermission]

Marc
 
M

Marc Gravell

(assuming your winform sets the principal at start)
And before you ask:

Thread.CurrentPrincipal = new
WindowsPrincipal(WindowsIdentity.GetCurrent());

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top