Link to home
Start Free TrialLog in
Avatar of jwebster77
jwebster77

asked on

Get the username in MVC web app

Hello,

I am trying to get the username in an MVC Web app which is published to an IIS, but it does not work.  
These are the 3 things I have tried:
1)
public string userName = System.Environment.UserName;

Open in new window


2)
public string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Open in new window


3)
In the view:
@Html.Label((string)ViewBag.Name)

Open in new window

In the controller:
ViewBag.Name = userName;

Open in new window



It works when I debug the app locally on my machine.  Although, when I go to the website it does not get my username, instead it looks like it gets something from the IIS?  I simply would like to get the username of the person on the website and no other functionalities.  Could you please help me with this?  Thank you very much in advance.
Avatar of Eduard Ghergu
Eduard Ghergu
Flag of Romania image

Hi,

Username can be gathered from HttpContext.Current.User.Identity.Name

Hi,


Assuming you have some kind of authentication in place and you are using .Net Core, have you tried this - On the View

@User.Identity.Name

Open in new window


On the Controller

HttpContext.User.Identity.Name

Open in new window


Regards,

Chinmay.

Avatar of jwebster77
jwebster77

ASKER

Hi, I am not using core.
Also, in what location of the view I would add "@User.Identity.Name"? Thank you.

Anywhere in the HTML blocks, you can put 

@User.Identity.Name 

Open in new window


If it does not resolve or gives you an error w.r.t. missing reference or assembly you will have to add appropriate references to the View. 

I took your suggestion and have the below in the controller:

    public class TicketsController : Controller
    {
        public string userName;
        private SqlConnection con;

        public ActionResult ViewAll()
        {
            userName = HttpContext.User.Identity.Name;
            ViewBag.Name = userName;

Open in new window



And this in the view:

@model IEnumerable<HelpDeskSupport.Models.Ticket>

@{
    ViewBag.Title = "ViewAll";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<head>...</head>

@User.Identity.Name
@Html.Label((string)ViewBag.Name)

<table><tbody>...</tbody></table>

Open in new window



But it still does not work.  This way it is not even working in debug...  Thank you.

Which authentication / authorization mechanism is being used in your project?

This is my first MVC app and do not think I have used any authentication/authorization?  I can tell you that what I see in the IIS I published the app to is what it is in the photo attached.  Please let me know if I have to do or try anything else?  Thanks.
User generated image
Any other ideas?  I also tried this in the controller:
ViewBag.Name = Request.RequestContext.HttpContext.User.Identity.Name;

Open in new window


and this in the view:
@Html.Label((string)ViewBag.Name)

Open in new window


but the label fills up with the username in debug but not when I run the website.  I also saw on another forum to go to the project properties and set Anonymous Authentication and Windows Authentication like in the photo;  I am not sure if it would make any difference.  Please help
User generated image

Hi,

Windows Authentication can be used just for intranet scenarios, and the users should be enrolled in AD. For other scenarios, you should go to IIS Manager and activate ASP.NET Impersonation. In the config file you should have Forms Authentication. You'll find a turial here: https://dotnettutorials.net/lesson/forms-authentication-in-mvc/

What does "AD" stand for?
I found a solution.  I had to make (1)Anonymous Authentication to Disabled  and  (2)Windows Authentication to Enabled in both the project properties and in the IIS Authentication.

Now I am getting my last name back "Webster" but I would like to get JWebster(first initial of my first name and last name) or Fieldaero\jim.webster.  How could I do that?
This is the code I have:

In the Controller:
var userName = Request.RequestContext.HttpContext.User.Identity.Name;
ViewBag.Name = userName;

Open in new window


In the View:
@Html.Label((string)ViewBag.Name)

Open in new window


If I do it directly from the view with code
@Request.RequestContext.HttpContext.User.Identity.Name

Open in new window

I get what I want, that is Fieldaero\jim.webster,  but I do not know how to get the same result in the controller.  I will then use the value in the controller to execute other code.  Thank you very much for your help!

Hi Jim,


Did you give this one a try in the controller?


string userName = HttpContext.User.Identity.Name

Open in new window


Regards,

Chinmay.

Hi,

AD means Active Directory and "Fieldaero\jim.webster" means the user "jim.webster" in AD domain called Fieldaero.

As I have suggested at the beginning of the discussion, you can use

HttpContext.Current.User.Identity.Name

in the controller


Hi, I did use  HttpContext.User.Identity.Name but I only get back Webster,  not JWebster nor Fieldaero\jim.webster.  Do I have to do anything else?  Thank you.

Hi,

Are you using IISExpress? Try to use the local IIS instead.

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.