Get Windows User Name via ASP.NET

G

Greg

I do see other postings here about getting the current Windows User Name.
But, they don't seem to apply to me. At least I don't think they do.

I want to be able to get the Current Windows User Name from what ever
computer is loading my web page. For example. On my development computer when
I used the following code:

string strUserName = WindowsIdentity.GetCurrent().Name.Substring(

WindowsIdentity.GetCurrent().Name.LastIndexOf(@"\") + 1);

it returns my current Windows Login Name.

But, once I move my web-site aspx files to the web server it returns
"NETWORK SERVICE" all the time.

I don't this for Security purposes. I'm only using this information to know
what Windows User Accounts are accessing web pages. What am I doing wrong?
And, how can I get the current Windows User Name for the computer requesting
the web page?

Thanks.
 
A

Alberto Poblacion

Greg said:
[...]
WindowsIdentity.GetCurrent().Name.LastIndexOf(@"\") + 1);
[...]
But, once I move my web-site aspx files to the web server it returns
"NETWORK SERVICE" all the time.
[...] What am I doing wrong?

You are getting the identity of the process that is executing the code. If
this is the code-behind in an asp.net web page, this defaults to the
identity of the asp.net worker process, NOT the user that sits behind the
brwser that requested the page.
And, how can I get the current Windows User Name for the computer
requesting
the web page?

First, enable Integrated Windows Authentication in IIS. Second, set the
<authorization> section in web.config so that anonymous access is denied for
the page where you are trying to find the identity of the user. Third,
verify that <authentication> is set to mode="windows". Then, you can get the
identity of the windows user by means of:

string username = User.Identity.Name;

This only works for users that are browsing by means of Internet
Explorer inside the local intranet. Other browsers do not support Integrated
Windows Authentication. Internet Explorer defaults to not sending the user
identity if the server is not in the local intranet (but this can be
reconfigured).
 
J

Jesse Houwing

Hello Alberto,
This only works for users that are browsing by means of Internet
Explorer inside the local intranet. Other browsers do not support
Integrated Windows Authentication. Internet Explorer defaults to not
sending the user identity if the server is not in the local intranet
(but this can be reconfigured).

That is not true. Almost any browser out there supports Integrated Security,
but most of them don't forward the current identity automatically, but ask
the user to enter their credentials. This is a security feature.

In firefox this can even be configured, so that it will pass the current
credentials to cerytain websites and domain, but not to any other.

Of the browsers I've worked with, these all support integrated security:
Firefox, Opera, Chrome, Safari (at least on windows).
 
L

Larry Smith

I want to be able to get the Current Windows User Name from what ever
computer is loading my web page.

You need to elaborate more on who your clients are. If your app is
targetting the Internet for instance, then what user name are you expecting
if the request isn't coming from a machine running Windows.
 

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

Top