locked
capturing Windows username RRS feed

  • Question

  • User-68560614 posted

    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:

    in GLOBAL.asax.cs

    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)
    {
    System.Security.Principal.WindowsIdentity user;
    user = System.Security.Principal.WindowsIdentity.GetCurrent();
    SaveCookie("ABKUser", user.Name.ToUpper());
    strSplitArr = user.Name.ToUpper().Split('\\'); // user is local (when testing)
    }

    AXTEST.Global.UserID = strSplitArr[1];

    Monday, October 22, 2012 1:32 PM

Answers

  • User365699807 posted

    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" to verify.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 1:38 PM
  • User365699807 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2012 6:14 PM

All replies

  • User365699807 posted

    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" to verify.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 1:38 PM
  • User-68560614 posted

    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.

    Monday, October 22, 2012 4:12 PM
  • User365699807 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2012 6:14 PM