1

How is this possible? Using MVC Razor, I marked my method with the [Authorize] attribute, which means that a user must be logged in to access the view right? But, sometimes I am getting the User.Identity.Name value as a null (so who's logged in then?).

my method does not have any roles defined in the authorize attribute, its like so:

[Authorize]
public ActionResult PageToView(){
...
return View();
}

EDIT: I am using the default Role and Membership providers.

robasta
  • 4,621
  • 5
  • 35
  • 53
  • 1
    What authentication mechanism are you using for your website? Which role provider are you using? There's a couple of possibilities so lets try and narrow it down :-) – dash Aug 20 '12 at 09:53
  • 1
    Have you tried adding Glimpse (http://getglimpse.com/) to your project? That would help you see at which point the `Context.User` becomes null (if it ever does). – Tieson T. Aug 20 '12 at 10:01
  • @rob and are you using Forms Authentication? The workflow can be quite tricky; there always exists an interval between the authentication and the response back to the client where the user is setup but it's not really ready to use yet. It could be you are hitting this: http://stackoverflow.com/questions/1056487/httpcontext-current-user-identity-name-is-always-string-empty – dash Aug 20 '12 at 10:35

1 Answers1

4

I had a similar issue before and it was because the HttpContext.Current.User was never being populated.

Please have a look at HttpContext.Current.User.Identity.Name returning null.

I hope that helps.

Community
  • 1
  • 1
Faisal
  • 83
  • 4