6

"Have you ever done any .net programming? Yes? Good, here's a massive broken program, fix it". That is the situation I'm in, so sorry if it's an easy question.

The program I am working on pulls a file from a web server. It is expected that the user is already logged into the web server. I need to pull the username of the current person logged into the server (or just make sure someone is indeed logged into the server).

I have tried the following and it returns an empty string.

user = HttpContext.Current.User.Identity.Name;
babno
  • 267
  • 2
  • 6
  • 15

3 Answers3

7

Please make sure you are setting windows authentication in Web.Config file. Also check the following before accessing the username,

HttpContext.Current.User.Identity.IsAuthenticated

Set Web.Config as follows,

<authentication mode="Windows"></authentication>
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • authentication is currently set to forms. Is there much danger in changing that to windows? – babno Jul 03 '12 at 11:32
  • 1
    +1 but you do not need to change to Windows. Keep the authentication mode to what the site has been designed for. (user name will be populated with any other authentication mode too, if logged in) – Adriano Repetti Jul 03 '12 at 11:34
  • 1
    Yes. Keep the design as it is. And try to understand formsauthentication using following link http://stackoverflow.com/questions/8810496/forms-authentication-understanding-context-user-identity – Rajesh Subramanian Jul 03 '12 at 11:35
1

First check in Web.config file for <authentication> tag. If you don't find it then your application may not be using any standard authentication mechanism. If that is the case look inside the login.aspx or whatever code that does the authentication. There you will get hold of logged in user data.

I wouldn't recommend you to change anything in web.config file without having some firm grasp on whats going in the application.

jorel
  • 808
  • 7
  • 15
0

The properties of the User object are usually populated by the application's authentication scheme (Forms, Windows or Custom) so you'll need to ensure one of these is in place before commencing to access the User.

For more information, take a look at the docs at MSDN.

immutabl
  • 6,857
  • 13
  • 45
  • 76