asked on
Get the username in MVC web app
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;
2)
public string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
3)
In the view:
@Html.Label((string)ViewBag.Name)
In the controller:ViewBag.Name = userName;
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.
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
On the Controller
HttpContext.User.Identity.Name
Regards,
Chinmay.
ASKER
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
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.
ASKER
public class TicketsController : Controller
{
public string userName;
private SqlConnection con;
public ActionResult ViewAll()
{
userName = HttpContext.User.Identity.Name;
ViewBag.Name = userName;
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>
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?
ASKER
ASKER
ViewBag.Name = Request.RequestContext.HttpContext.User.Identity.Name;
and this in the view:
@Html.Label((string)ViewBag.Name)
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
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/
ASKER
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;
In the View:
@Html.Label((string)ViewBag.Name)
If I do it directly from the view with code
@Request.RequestContext.HttpContext.User.Identity.Name
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
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
ASKER
Hi,
Are you using IISExpress? Try to use the local IIS instead.
Hi,
Username can be gathered from
HttpContext.Current.User.Identity.Name