Connecting Tech Pros Worldwide Forums | Help | Site Map

finding the current user name accessing an ASP.NET page

Joe
Guest
 
Posts: n/a
#1: Mar 10 '06
This may actually be an IIS configuration issue but any help would be
appreciated.
I'd like to display some content based upon who the current user is that is
accessing an internal ASP.NET applciation. I am using the 2.0 framework. I
use the following syntax to retrieve the user name:

string username = Environment.UserName;

I use the 'username' variable to make comparisons and show the appropriate
content based upon the user. This works fine in a test enviornment (running
IIS 5.1). When I move it over to production (IIS 6.0) this code snipet
returns a single user called "NETWORK SERVICE" regardless of who or what
machine attempts to access the web page.

Any ideas on how I could return the appropriate user instead of "NETWORK
SERVICE" in a production Win2k3/IIS 6.0 enviornment?

Thanks for your help.

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Mar 10 '06

re: finding the current user name accessing an ASP.NET page


Joe,

Assuming you have authentication set up correctly, why not use the User
property (which returns an IPrincpal implementation) on the Page class, or
on the HttpContext class?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Joe" <thejoe@community.nospam> wrote in message
news:9F8BB808-FAB1-494B-BD3E-EAC94187C947@microsoft.com...[color=blue]
> This may actually be an IIS configuration issue but any help would be
> appreciated.
> I'd like to display some content based upon who the current user is that
> is
> accessing an internal ASP.NET applciation. I am using the 2.0 framework. I
> use the following syntax to retrieve the user name:
>
> string username = Environment.UserName;
>
> I use the 'username' variable to make comparisons and show the appropriate
> content based upon the user. This works fine in a test enviornment
> (running
> IIS 5.1). When I move it over to production (IIS 6.0) this code snipet
> returns a single user called "NETWORK SERVICE" regardless of who or what
> machine attempts to access the web page.
>
> Any ideas on how I could return the appropriate user instead of "NETWORK
> SERVICE" in a production Win2k3/IIS 6.0 enviornment?
>
> Thanks for your help.[/color]


Tom Porterfield
Guest
 
Posts: n/a
#3: Mar 10 '06

re: finding the current user name accessing an ASP.NET page


Joe wrote:[color=blue]
> This may actually be an IIS configuration issue but any help would be
> appreciated.
> I'd like to display some content based upon who the current user is that
> is accessing an internal ASP.NET applciation. I am using the 2.0
> framework. I use the following syntax to retrieve the user name:
>
> string username = Environment.UserName;
>
> I use the 'username' variable to make comparisons and show the appropriate
> content based upon the user. This works fine in a test enviornment
> (running IIS 5.1). When I move it over to production (IIS 6.0) this code
> snipet returns a single user called "NETWORK SERVICE" regardless of who
> or what machine attempts to access the web page.
>
> Any ideas on how I could return the appropriate user instead of "NETWORK
> SERVICE" in a production Win2k3/IIS 6.0 enviornment?
>
> Thanks for your help.[/color]

That is because your web site is running under the NETWORK SERVICE account
on IIS6.0, whereas in the VS debugger it is running as you.

In your web you can use HttpContext.Current.User.Identity.Name or the ID on
the context. You can also access Thread.CurrentPrincipal.Identity.Name.
This would require that you do not allow anonymous access to your site,
otherwise the value will be null.
--
Tom Porterfield

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#4: Mar 10 '06

re: finding the current user name accessing an ASP.NET page


Hi,


[color=blue]
> Any ideas on how I could return the appropriate user instead of "NETWORK
> SERVICE" in a production Win2k3/IIS 6.0 enviornment?[/color]

First you have to configure your IIS , you go to the property of the
app/security and disable anonymous access.

Then in your web.config you have to set it to inpersonate and set the
authentication to windows:


<authentication mode="Windows"/>
<identity impersonate="true"/>

finally you get the login with WindosIdentity.GetCurrent , usually in the
session_Start:

protected void Session_Start(Object sender, EventArgs e)
{
string login = WindowsIdentity.GetCurrent().Name.Substring(
WindowsIdentity.GetCurrent().Name.LastIndexOf(@"\" )+1 );
Session["SystemUser"] = login;
}

I do remove the domain part.




--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Joe Campbell
Guest
 
Posts: n/a
#5: Mar 15 '06

re: finding the current user name accessing an ASP.NET page


thanks all. I got it working. I used Ignacio's post to get it running.

"Joe" wrote:
[color=blue]
> This may actually be an IIS configuration issue but any help would be
> appreciated.
> I'd like to display some content based upon who the current user is that is
> accessing an internal ASP.NET applciation. I am using the 2.0 framework. I
> use the following syntax to retrieve the user name:
>
> string username = Environment.UserName;
>
> I use the 'username' variable to make comparisons and show the appropriate
> content based upon the user. This works fine in a test enviornment (running
> IIS 5.1). When I move it over to production (IIS 6.0) this code snipet
> returns a single user called "NETWORK SERVICE" regardless of who or what
> machine attempts to access the web page.
>
> Any ideas on how I could return the appropriate user instead of "NETWORK
> SERVICE" in a production Win2k3/IIS 6.0 enviornment?
>
> Thanks for your help.[/color]
Closed Thread