finding the current user name accessing an ASP.NET page

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 
Joe said:
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.

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.
 
Hi,


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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top