Solved

Username

Posted on 2011-03-03
11
250 Views
Last Modified: 2012-05-11
Hi Experts

Is this the best way to get the name of a user using an app on any machine?....

Label lblUsername = new Label();
lblUsername.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

im then inserting this data with other info into db.

Thanks
0
Comment
Question by:razza_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 13

Expert Comment

by:Rahul Agarwal
ID: 35025648
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35025654
this is indeed the Way ;-)

you're showing the Users windows Logon name in the textbox

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx

regards
poor beggar
0
 
LVL 9

Expert Comment

by:AnilKumarSharma
ID: 35025665
Yes but your application that get user name needs admin privileges.
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 1

Author Comment

by:razza_b
ID: 35025729
When i use in my dev env i get CORPORATE\name, but in live the saved user in db is
SR-PNG-WIP02\IUSR_SR-PNG-WIP02
0
 
LVL 3

Expert Comment

by:pasolo
ID: 35025799
Environment.UserName (System.Environment)

is more strightforward
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35025960
That would suggest that you are talking about a Web Application rather than a Desktop app. You must have anonymous access enabled for your site, which is causing the Identity of the account that IIS runs under to be returned, rather than the domain account that is accessing the site.

In IIS Admin, you need to switch off the Allow Anonymous Access option for the site.
0
 
LVL 1

Expert Comment

by:vandalesm
ID: 35026834
I agree with carl, you must use Window Authentication instead of Anonymous access.
0
 
LVL 6

Expert Comment

by:TMWSIY
ID: 35056668
If this is a web app just use :

User.Identity.Name

Open in new window

0
 
LVL 1

Author Comment

by:razza_b
ID: 35084490
sorry guys not been @ work will update asap.
0
 
LVL 1

Author Comment

by:razza_b
ID: 35108604
Hi Guys

I have tried all 3 of these and still no luck. What appears in db table sis below...

User.Identity.Name; = empty
System.Environment.UserName; = IUSR_SR-PNG-WIP02
System.Security.Principal.WindowsIdentity.GetCurrent().Name; = IUSR_SR-PNG-WIP02

Thanks
0
 
LVL 6

Accepted Solution

by:
TMWSIY earned 500 total points
ID: 35108783
IUSR_%computerName% is the users IIS uses for anonymous users.  If you want to capture the user name you need to enable impersonation.

Impersonate the IIS Authenticated Account or User:

To impersonate the Microsoft Internet Information Services (IIS) authenticating user on every request for every page in an ASP.NET application, you must include an <identity> tag in the Web.config file of this application and set the impersonate attribute to true. For example:
<identity impersonate="true" />

It seems that you have this set on your dev machine but not in production.

http://support.microsoft.com/kb/306158
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question