MVC5 User.Identity.GetUserName() is Null in Controller

httpcontext.user.identity.name is null .net core
asp.net core get current user outside controller
asp.net core identity get current user
user identity name is null asp net core
asp.net get current user identity
httpcontext.current.user.identity.name returns null
httpcontext is null in controller constructor
user identity getuserid returns null

What's going on here? User.Identity.GetUserName() works fine in a view, e.g in the logon partial view. But if I use it in a controller it throws a null reference exception.

I'm looking at the page, and the UserName is displayed in the header, then if I click on a link to a controller where I've tried to use it I get a null reference exception.

I should add, if I do it this way it works fine:

        var httpContext = System.Web.HttpContext.Current;
        _userName = httpContext != null ? httpContext.User.Identity.Name : "Testing";

So mostly I'm just curious.

Where did you put the code to invoke User.Identity.GetUserName();? If it was on constructor then you'll get that null reference exception -- cause object User is not initialized yet in the constructor.

ASP.NET MVC - BaseController, an error that the HttpContext.User.Identity.Name is null, but if it's on the page controller the Name i. https://www.code-sample.com/2014/08/base-controller-in -mvc-5.html. My usage of a + User.Identity.GetUserName() + "! There are two simple ways to get current user in MVC 5. If you are inside the controller class current user can be fetched as follows, string userId = User.Identity.GetUserId (); Do not forget to add namespace,

The User object is exposed to the Controller, so you don't need to access it via System.Web.HttpContext.Current.

var myUserName = User.Identity.GetUserName();

HttpContext.User Property (System.Web), If the application uses Windows authentication, the user name includes the domain. For example, the page title would be "Home page for DOMAIN\ username". public void SigninToSignalR() { // this's always null string userId = HttpContext.Current.User.Identity.GetUserId (); } Actually, the user is not signed in - not in the context of the current request (the POST /Account/Login request), which is where User.Identity gets its data.

This works.

public static class GetUserName
{
    public static string Name(IPrincipal user)
    {
        return user==null? HttpContext.Current.User.Identity.Name:user.Identity.Name;
    }
}

Adding ASP.NET Identity to an Empty or Existing Web Forms Project , NET Identity support to your existing Web Forms project or a new empty project. var user = new IdentityUser() { UserName = UserName. controller = new MyController() { GetUserId = => "IdOfYourChoosing" }; There are pros and cons to both approaches. The first approach is more thorough and more flexible and uses seams that are already present on the two controller types, but it’s also a lot more code and doesn’t read nearly as well.

Also make sure that you don't have

<authentication mode="None" />

in your Web.config like I just had.

Asp.net MVC identity problem, MVC5 � logon. So i just created a testing app in visual studio 2015 Mvc; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity. private ApplicationUserManager _userManager; public AccountController() Email); if ( user == null) { // Don't reveal that the user does not exist GetUserName() + "! In that context, I am creating a MVC based ASP.NET Web Application and I am also assuming that you have the knowledge necessary to configure the social login provider in your Web Application. You can visit Configure the External Login Options in MVC as a reference to access the external authentication for your ASP.NET Web Application.

User.Identity.GetUserId() returns null after successful login, User.Identity.GetUserId() always returns null in controller action , User.Identity. User.Identity.Name or GetUserName not working on MVC 5, First of all if you� I have a MultipartForms in Which I Could upload an Image and other form values.While, the form values are rightfully received through the FormCollection Property whereas the Upload file always shows the null value in HttpPostedFileBase Property.I go through forums but I couldn't get Where went Wrong.

User.Identity.Name is null after login � Issue #14160 � dotnet , Identity.Name is null after login Using .NET Core 3.0 rc1. I created a But User. Identity.Name returns null in my controller. I've tried httpContextAccessor, but it is still null. And the calls to AddCookie() and AddIdentityServer. Introduction. AspNet Identity 2.0 with MVC 5 by default displays the username which is user's email address in _LoginPartial.cshtml page. I will go through how to change this to user's FirstName and LastName i.e. Full Name

User.Identity.Name is always null � Issue #79 � IdentityServer , I'm able to login using my existing Identity DB and that's all good but I For example from a MVC controller User.Identity.GetUserId() and the� I need to be able to construct a link in the Action on the controller to send an email. What is best practice to do this? I don't want to construct it myself in case my routes change. Should I have a view for each email and render that and send it? That might be a good way of doing it

Comments
  • I have the same problem:(
  • That's precisely what I did Brendan, and it was throwing a null reference exception.
  • Is the controller flagged as AllowAnonymous? If you have a view from the same controller working fine, then there may be something else going on.