0

I have this application I need to get the name of the windows logged user. I hosted this on a server. When I am tested locally this works perfectly but when I hosted on the server the program returns the server name, not the logged user username. My code is mentioned below

Index.cshtml

<h1>Hello @ViewBag.UserName</h1>

Controller

public ActionResult Index()
{
    string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    string x = "";
    x = name.Split('\\')[1];
    ViewBag.UserName = x;
    return View();
}

Web.config

<system.web>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" />
      <authentication mode="Windows"/>
  </system.web>

Any help is appreciated. Thank you.

rrrrTTTyyy
  • 39
  • 6
  • You _do understand_ why getting the **Windows username** of a remote-user (outside of a Windows Active Directory Domain LAN environment) is impossible, right? (If not, then ask yourself what should happen if someone is using an iPhone, Linux computer, or non-Domain PC to access your website). – Dai Jan 19 '22 at 10:35
  • The system only is accessed with windows PC no Linux or apple devices. And all the PCs are In the domain. No outside domain users can use this – rrrrTTTyyy Jan 19 '22 at 10:39
  • @rrrrTTTyyy Try this: `string name = System.Web.HttpContext.Current.User.Identity.Name;` – Rahul Sharma Jan 19 '22 at 11:01
  • This is locally working but when I run it on the server it will give me an error @RahulSharma – rrrrTTTyyy Jan 19 '22 at 11:15
  • @rrrrTTTyyy You can either configure IIS in Control Panel so that your site (or machine) uses Windows authentication and denies anonymous access or you can add the following to your web.config in the system.web section: ` ` – Rahul Sharma Jan 19 '22 at 11:20
  • Edited the web.config now I am getting a 401 unauthorized error @RahulSharma – rrrrTTTyyy Jan 19 '22 at 11:35
  • @rrrrTTTyyy Okay, you can find more information on this here: https://stackoverflow.com/questions/1571036/httpcontext-current-user-identity-name-is-empty – Rahul Sharma Jan 19 '22 at 11:36
  • I tried this but I cant figure this one out the program is working in my personal computer but the domain computer is asking for a username and password and the server is giving an error 401 @RahulSharma – rrrrTTTyyy Jan 19 '22 at 11:51
  • @rrrrTTTyyy You have to set authentication mode to Windows in your configuration & also disable anonymous users in authorization tag and then try again – Rahul Sharma Jan 19 '22 at 11:59

1 Answers1

0

Since you're using MVC in .Net framework, you can get the user's name/identity using:

string name = User.Identity.Name;

The reason it's returning the server is that WindowsIdentity.GetCurrent() returns the identity under which the thread is running, while User.Identity returns the identity which was passed to IIS.

haldo
  • 14,512
  • 5
  • 46
  • 52
  • I tried this locally and on the server but this returned nothing `public ActionResult Index() { string name = User.Identity.Name; //string x = name.Split('\\')[1]; ViewBag.UserName = name; return View(); }` Do I need change the web.config – rrrrTTTyyy Jan 19 '22 at 10:26
  • @rrrrTTTyyy make sure you've disabled anonymous authentication and enabled windows auth in your project settings (not just in webconfig) and that you're actually authenticating the user. – haldo Jan 19 '22 at 10:34
  • 1
    Also ensure you're using the _full fat_ IIS and not IIS Express. – Dai Jan 19 '22 at 10:40
  • Both of these things should be done in Visual Studio am I right? @haldo – rrrrTTTyyy Jan 19 '22 at 10:48
  • @rrrrTTTyyy In your config – JamesS Jan 19 '22 at 10:49
  • Click the project in VS and check the properties pane - press F4 if properties pane isn't open. You should see settings for anonymous/windows auth in the properties pane @rrrrTTTyyy – haldo Jan 19 '22 at 10:51
  • I change the settings now I am getting a login prompt asking for username and password when I run in locally and in the server it is only displaying an error @haldo – rrrrTTTyyy Jan 19 '22 at 11:20
  • How can I get that because I am using the IIS Express @Dai – rrrrTTTyyy Jan 19 '22 at 11:53
  • I just created a new MVC app, configured windows auth, disabled anonymous auth, added windows auth in webconfig and my name was displayed when running locally in IIS express. Are you sure you're on a domain @rrrrTTTyyy? When I'm not on a domain I get a popup asking for credentials like you – haldo Jan 19 '22 at 11:55
  • @rrrrTTTyyy https://learn.microsoft.com/en-us/iis/application-frameworks/scenario-build-an-aspnet-website-on-iis/configuring-step-1-install-iis-and-asp-net-modules – Dai Jan 19 '22 at 12:04
  • @rrrrTTTyyy also take a look at https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/authentication/windowsauthentication/ – haldo Jan 19 '22 at 12:05
  • I am on a domain that pc is asking for credentials. But when I try this non-domain PC then the name will display @haldo – rrrrTTTyyy Jan 19 '22 at 12:14