1

hope if someone can help me. i have a simple webpage nad i want to display a windows user name on my webpage. i just need the user name from the windows on my webpage.

i had use some codes do do that but no result like

HttpContext.Current.User.Identity.Name

but it is giving me empty string

also i have tried

system.envoirement.username

its work when i run it from vistual studio but this code not give me the right answer when ii run it from IIS. i am going to use this webpage as my Intranet on local network.

any reply will be appreciated.

Thanks in advanced.

I kiet
  • 176
  • 2
  • 12

2 Answers2

0

Programmatically, you are looking for User.Identity.Name which inherits from System.Web.HttpContext.

myLabel.Text = User.Identity.Name

Be sure IIS is set up for some type of windows authentication, (as SLaks indicated), rather than anonymous authentication.

Also, see this SO link: System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET

Community
  • 1
  • 1
MTAdmin
  • 1,023
  • 3
  • 17
  • 36
  • Thanks for quick reply, i have tried this line of code before but it is giving me empty string. i know that might be my question is bit silly. but i dont know where i am going wrong. as i have done this in past , but nothing is working this time... :( – I kiet Dec 17 '12 at 16:39
  • I believe VS pulls the user name of the currently logged in user on your development machine, so how are you authenticating on your actual site? Can you tell us what IIS authentication settings you are using, as well as show us your code to assign the identity to your HTML control? – MTAdmin Dec 17 '12 at 16:53
  • connection.Open() Dim MyReader As SqlDataReader = comm.ExecuteReader() If MyReader.Read() Then Dim User_id As String = MyReader.GetValue(0).ToString Session("User_ID") = user_id response.redirect("nest_pasge.aspx") else response.redirect("Error_page.aspx") end if connection.close() – I kiet Dec 17 '12 at 17:04
  • IIS SETTING IS SET TO APPLICATION POOL AS I AM GIVING TEH USER NAME AND PASSWORD IN THE CONNECTION STRING TO GET ACCESS INTO THE DATABASE. – I kiet Dec 17 '12 at 17:05
  • Have you verified whether you are authenticating? Something like, If User.Identity.IsAuthenticated Then Label1.Text = User.Identity.Name Else Label1.Text = "Anonymous user" End If – MTAdmin Dec 17 '12 at 17:12
  • yes i did try that, and that is the point where i am loosing everything because when i am doing if User.Identity.IsAuthenticated its gives me false althought its supose to be true....:D because i did check the user before that...! – I kiet Dec 17 '12 at 17:15
  • I would direct your efforts toward your web server. Authentication = false indicates that you need to check that your authentication type is set up correctly. Are you being prompted for authentication when you request your page from your IIS site? – MTAdmin Dec 17 '12 at 17:23
0

The only thing you need to do to get the user name is with the following code:

 HttpContext.Current.User.Identity.Name

If you are using Visual Studio 2012, then from your project setting you have to turn SSL to true.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
I kiet
  • 176
  • 2
  • 12