How to get User.Identity.Name from a controller?
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.
chobo2
Updated on June 04, 2022Comments
-
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 over 14 yearsWhy the down vote? It works for me? Although I'm happy to take the downvote if it means I'm learning something new...
-
chobo2 over 14 yearsGet 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 over 14 yearsNot 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 over 14 yearsSee Wyatt's comment above. Make sure you call base.Initialize().
-
chobo2 over 14 yearsHmm 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 over 14 yearsHe's not on a Page. He's creating a custom Controller.
-
David over 14 yearsGood points. Any of those issues would definietly cause the error described.
-
chobo2 over 14 yearswhere do I call this base.Initialze()? I am not sure where to stick it
-
chobo2 over 14 yearsUser is Null. That is what is null
-
chobo2 over 14 yearsOh and ya I am logged in. If I take out that line and try to log in I go to authorized pages.
-
chobo2 over 14 yearsWhere do I stick it in the constructor?
-
chobo2 over 14 yearsIt also wants some parameter and I am not sure how to pass in what it wants.