I have an ASP.Net MVC 5 project that I recently migrated from MVC 4. I'm using FormsAuthentication with HTTPS and anonymous authentication, and setting a cookie using this:
FormsAuthentication.RedirectFromLoginPage(Username, false)
Authentication works, but the issue I'm having is getting the current user's name in my C# code. I can get at it fine in my view,
User.Identity.GetUserName()
but I can't get at it in the controller. HttpContext.CurrentUser doesn't exist, and this.User in the controller is always null. I've also tried requesting the cookie, but it fails.
Will I have to set the Identity myself when they sign on? I may need user rolls, but I could always query the database to get them.
In short, how can I get the current user's username in my controller in MVC 5? Thanks
WebMatrix.WebData;as well. If the user isn't authenticated it returns an empty string. stackoverflow.com/a/17254386/4051272RedirectFromLoginPagesets a cookie for you, but I checked on another controller and can get atUser.Identitynow. My problem was that I was posting to the home controller, then calling another controller's method like this(new MyController()).MyMethod(myModel);. Time to change that lol. Thanks for bringing that up, I never woulda figured that out.