1

I have an ASP.net app which is being hosted on a server. I want to get the Clients username from the system and use that name as a title for an XML file im saving on the server. I have tried various different mathods such as: Environment.UserName(); WindowsIdentity.GetCurrent();

However all that keeps returning is the NetworkService account name on the server.

Can anyone assist with this.

thank you

1
  • The methods you used gives you the user that is running the thread handling the user`s request. That is by default the user that is configured to run the application pool that the app in running in. What you need is sonthing else, that the guys here pointed out how to get. Commented Mar 7, 2011 at 13:40

5 Answers 5

7

Given that your application has authentication set up (either Forms or Windows Authentication), you can get the current user's name via:

HttpContext.Current.User.Identity.Name
Sign up to request clarification or add additional context in comments.

1 Comment

Tried this but it still returns the NetwrokService account
1

Your web application is running (by default) under the NetworkService account. If you want to know the username of the user's NT account you should check out impersonation. With that the application will be running with the credentials of the user.

For public websites this is not possible or shouldn't be done. But internal company applications could do so.

Comments

1

Try this:

HttpContext.Current.User.Identity.Name

This will only work if a user is authenticated by either Forms/Membership authentication or Windows, you can check beforehand:

string userName = "Anonymous";
if (User.Identity.IsAuthenticated)
   userName = HttpContext.Current.User.Identity.Name

Comments

1

If your users are authenticated you can use User.Identity.Name

Comments

0

you want the user name of the web client and WindowsIdentity or Env.UserName return you the server side user name, which might be the same as the web client user name only if you have enabled authentication.

I think to solve your issue you should look in the Request headers, in one of those headers there should be a username or useragent string telling you what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.