Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# asp.net getting username

Tags:

c#

asp.net

"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;
like image 372
babno Avatar asked Jul 03 '12 11:07

babno


2 Answers

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>
like image 94
Rajesh Subramanian Avatar answered Sep 18 '22 02:09

Rajesh Subramanian


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.

like image 41
jorel Avatar answered Sep 19 '22 02:09

jorel