Last post Oct 23, 2012 06:14 PM by HenryLiang
Oct 22, 2012 01:32 PM|mbraganca|LINK
I have built a web app using Visual Studio 2010 (Ver 10.0.40219.1 SP1 Rel) .Net 4.0
On my site master page I want to display the current user's windows Id.
When I run the app from my workstation the userID is display as expected.
However when I deploy it to my web server (WS2003 / IIS6), The UserID is displayed as NETWORK SERVICE.
I have set Integrated Windows authentication ON.
What am I missing?
Here is the code I am using:
protected void UserID()
string strSplitArr = System.Web.HttpContext.Current.User.Identity.Name.ToString().Split('\\'); // used for remote user (via web server)
if (strSplitArr.Length == 1)
user = System.Security.Principal.WindowsIdentity.GetCurrent();
strSplitArr = user.Name.ToUpper().Split('\\'); // user is local (when testing)
AXTEST.Global.UserID = strSplitArr;
Oct 22, 2012 01:38 PM|HenryLiang|LINK
The code "System.Web.HttpContext.Current.User.Identity.Name" returns you the current login user id, and System.Security.Principal.WindowsIdentity.GetCurrent() returns you the current windows account that your application is running on. Therefore, when you
deploy your app on the production server, you should use "System.Web.HttpContext.Current.User.Identity.Name" instead of the 2nd one.
The reason that the app works on your local box is because you were using Visual Studio build-in web server, which is running on your account.
You can setup a breakpoint at this line: string strSplitArr = System.Web.HttpContext.Current.User.Identity.Name.ToString().Split('\\'); // used for remote user (via web server), and check the value of "System.Web.HttpContext.Current.User.Identity.Name"
Oct 22, 2012 04:12 PM|mbraganca|LINK
I realize that there are two different methods of getting the User ID (local and remote), which is why I have both. However the 1st method (System.Web.HttpContext.Current.User.Identity.Name) is not returning anything!
It is as if it is failing and not throwing an error just an empty string.
Oct 23, 2012 06:14 PM|HenryLiang|LINK
It's very weird. I would suggest you to check the IsAuthenticated property and see if the user is authenticated before get the User.Indentity.Name.
And what's version of IIS you are using? You need to enable Windows Authentication from IIS as well.
Hope this helps.