locked
WindowsIdentity.GetCurrent().Name not working on server RRS feed

  • Question

  • User257607616 posted

    Hi, everyone!

    I have two web apps (MVC) running in the intranet of the company that I work. Both run in the same server. The server runs IIS 7.5. There´s an Active Directory where users are authenticated in order to access the network resources, including web apps.

    For both apps I use Windows Authentication method. Both apps are restricted to registered users. So, the first thing that is performed in the apps is to identify the logged user and verify whether he/she is registered. I do this in the start action (Index) of the apps, in the HomeController. 

    using System.Web.Mvc;
    
    namespace ProjetoX.UI.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                Usuario oUser = UserConfig.GetUserApp();
                if (oUser != null)
                {
                    Session["oUser"] = oUser;
                    ViewBag.NomeUsuario = oUser.Nome;
                    return View();
                }
                else
                {
                    ViewBag.NomeUsuario = UserConfig.GetUserLogged().DisplayName;
                    return View("Restricao");
                }
            }
        }
    }

    To identify the logged user, I have the same code (listed bellow) in both apps.

    using System.Security.Principal;
    
    namespace ProjectX.UI
    {
        public class UserConfig
        {
            public static UsuarioAD GetUserLogged()
            {
                return new AcessoAD().ObtemDadosUsuario(WindowsIdentity.GetCurrent().Name.ToString().Substring(5));
            }
    
            public static Usuario GetUserApp()
            {
                return new UsuariosApp().LocalizaUsuario(WindowsIdentity.GetCurrent().Name.ToString().Substring(5));
            }
        }
    }

    So, if the logged user is registered in the app, it goes on. Otherwise, I get the name of the user in AD (AcessoAD().ObtemDadosUsuario(...) )and a page informing about the restriction is showed.

    Well, the first app, installed a few months ago, is running properly. It identifies the logged user correctly. But the most recent app I installed in the server does not identifies the user that is logged. Never. It runs correctly in my developing machine.

    In IIS Manager, in both sites in Authentication I have configured this way:

    • Anonymous Authentication  - Disabled
    • ASP.NET Impersonation        - Disabled
    • Forms Authentication           - Disabled
    • Windows Authentication      - Enabled 

    Well, as far as I know, in IIS Manager there's no other configuration that could interfere in the behavior of the app, concerning with the identification of the user.

    In order to try to check what is happening, I tried the code bellow to get what is being returned by WindowsIdentity.GetCurrent().Name.

    In controller:

    using System.Web.Mvc;
    
    namespace ProjetoX.UI.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                Usuario oUser = UserConfig.GetUserApp();
                if (oUser != null)
                {
                    Session["oUser"] = oUser;
                    ViewBag.NomeUsuario = oUser.Nome;
                    return View();
                }
                else
                {
                    ViewBag.NomeUsuario = UserConfig.LogonDoUsuario();
                    return View("Restricao");
                }
            }
        }
    }
    

    In class UserConfig

    using System.Security.Principal;
    
    namespace Investimento.UI
    {
        public class UserConfig
        {
            public static UsuarioAD GetUserLogged()
            {
                return new AcessoAD.Corp().ObtemDadosUsuario(WindowsIdentity.GetCurrent().Name.ToString().Substring(5));
            }
    
            public static Usuario GetUserApp()
            {
                return new UsuariosApp().LocalizaUsuario(WindowsIdentity.GetCurrent().Name.ToString().Substring(5));
            }
    
            public static string LogonDoUsuario()
            {
                return WindowsIdentity.GetCurrent().Name.ToString();
            }
        }
    }

    Then I rebuilt the app and publish it to the server. Well, I got the following as ViewBag.NomeUsuario

    NT AUTHORITY\NETWORK SERVICE.

    If is there anything else that I should provide, please let me know.

    Well, I hope I'll be enlightened by one of the fellows.

    Thanks for your attention.

    Paulo Ricardo Ferreira

    Monday, August 29, 2016 4:16 PM

Answers

  • User257607616 posted

    Hi, everyone!

    After a little more digging, I tried using System.Web.HttpContext.Current.User.Identity.Name instead of System.Secutiry.Principal.WindowsIdentity.GetCurrent().Name.

    This way the app got the username properly.

    Thanks for your attention.

    Paulo Ricardo Ferreira

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 29, 2016 6:38 PM