How to get User.Identity.Name from a controller?

14,386

Solution 1

Try initializing stuff in the ApplicationController's Initialize method, HttpContext isn't avaliable in the constructor. Make sure to call base.Initialize() or you can get some interesting results.

Solution 2

HttpContext.Current.User.Identity.Name  

Note the .Current that was missing from your call.

Solution 3

Which specific property that you are accessing is null? You might want to check that User.Identity.IsAuthenticated == true before checking the Name property. Also make sure that forms authentication is enabled and that you have indeed logged into the site first.

Share:
14,386
chobo2
Author by

chobo2

Updated on June 04, 2022

Comments

  • chobo2
    chobo2 almost 2 years

    I want to have some viewData info across all my views so I am following this tutorial

    http://www.asp.net/LEARN/mvc/tutorial-13-cs.aspx

    So I made my own applicationController but I need to get the UserName from the user.

    Yet when I do this "HttpContext.User.Identity.Name" in the constructor it is always null. I am not sure why though.

    Thanks

    Ok the base thing did the trick.

      protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            Guid userId = coursesS.GetUserId(HttpContext.User.Identity.Name);
        }
    

    So that seems to work. The only thing now is it seems to go through this twice. Like I log in and it seems to do the this twice. I am guessing since this is like the equivalent of putting that code in every action. When I am loading up all these partial view on my site by calling them in my controller it does this.

    So many I should cache it but I am not sure how though.

  • David
    David over 14 years
    Why the down vote? It works for me? Although I'm happy to take the downvote if it means I'm learning something new...
  • chobo2
    chobo2 over 14 years
    Get this error when I try to do this in my abstract base controller. Error 2 An object reference is required for the non-static field, method, or property 'System.Web.Mvc.ControllerContext.HttpContext.get'
  • chobo2
    chobo2 over 14 years
    Not sure why someone is voting you done. I tried it and I get this error: Error 3 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.User.get'
  • Keith Adler
    Keith Adler over 14 years
    See Wyatt's comment above. Make sure you call base.Initialize().
  • chobo2
    chobo2 over 14 years
    Hmm I might need to import something since I get this: Error 3 'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)
  • Keith Adler
    Keith Adler over 14 years
    He's not on a Page. He's creating a custom Controller.
  • David
    David over 14 years
    Good points. Any of those issues would definietly cause the error described.
  • chobo2
    chobo2 over 14 years
    where do I call this base.Initialze()? I am not sure where to stick it
  • chobo2
    chobo2 over 14 years
    User is Null. That is what is null
  • chobo2
    chobo2 over 14 years
    Oh and ya I am logged in. If I take out that line and try to log in I go to authorized pages.
  • chobo2
    chobo2 over 14 years
    Where do I stick it in the constructor?
  • chobo2
    chobo2 over 14 years
    It also wants some parameter and I am not sure how to pass in what it wants.