2 Replies - 9399 Views - Last Post: 10 June 2009 - 03:59 PM Rate Topic: -----

#1 smcherniss  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 8
  • Joined: 25-June 08

How to retrieve user network logon id

Post icon  Posted 10 June 2009 - 10:30 AM

How to retrieve user network logon id

I recently needed to retrieve the user's network logon id for a project I was working on. There are plenty of articles on the web discussing this, so I wont go into a great deal of detail. I will discuss problems and solutions I encountered.

I read I could retrieve the Network Logon Id from my website using the following function:

				string strTemp1 = string.Empty;
				string strTemp2 = string.Empty;

				// Either method should retrieve the network id 
				strTemp1 = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();
				strTemp2 = HttpContext.Current.Request.LogonUserIdentity.Name.ToString();




or


if ( Thread.CurrentPrincipal != null )
{
   // Obtain the authenticated user's identity
   // Cast Thread.CurrentPrincipal as a WindowsPrincipal Object
   WindowsPrincipal winPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
   
   // Retrieve the user's network logon name
   lblUserName.Text = winPrincipal.Identity.Name.ToString();
}				 




or


if ( HttpContext.Current.User != null )
{
   // Obtain the authenticated user's identity
   WindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;
   
   // Retrieve the user's network logon name
   lblUserName.Text = winPrincipal.Identity.Name.ToString();
}	 





In order for the aforementioned code to work my web.config file needed the following settings

web.config
   <identity impersonate="false"/>
   <authentication mode="Windows" />



After implementing these settings, it was not working and I did not know why. The problem was a setting in my web.config. I had the following setting configured in my web.config

<authorization>
	  <allow users="*"/>
	  <!-- Allow all users -->
	  <!--  <allow	 users="[comma separated list of users]"
							 roles="[comma separated list of roles]"/>
				  <deny	  users="[comma separated list of users]"
							 roles="[comma separated list of roles]"/>
			-->
	</authorization>



The <allow users="*"/> was causing my code to return a blank value.

I had to change my web.config file to include this configuration

<authorization>
	  <deny users="?"/>
	  <!-- <allow users="*"/> -->
	  <!-- <allow users="*"/> Does not allow the following function to work
			HttpContext.Current.Request.ServerVariables["AUTH_USER"]-->
	  
	  <!-- Allow all users -->
	  <!--  <allow	 users="[comma separated list of users]"
							 roles="[comma separated list of roles]"/>
				  <deny	  users="[comma separated list of users]"
							 roles="[comma separated list of roles]"/>
			-->
</authorization>



Instead of using <allow users="8"/> I needed to use <deny users="?"/>

Happy Coding

Is This A Good Question/Topic? 0
  • +

Replies To: How to retrieve user network logon id

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to retrieve user network logon id

Posted 10 June 2009 - 10:52 AM

you probably should have created a snippet or tutorial for this.
Was This Post Helpful? 0
  • +
  • -

#3 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: How to retrieve user network logon id

Posted 10 June 2009 - 03:59 PM

yes, this is an interesting topic. Please take this post and put it in a tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1