views:

39

answers:

1

I'm using Forms authentication on my MVC website. I administrate users and roles using the default ASP.NET Configuration option in Visual Studio. All good so far.

I can successfully do Page.User.IsInRole("Moderator") in a View. True is returned as expected.

However when calling Context.User.IsInRole("Moderator") inside Global.asax's Application_PostAuthenticateRequest() (or any later event during the cycle, for the matter..) False is returned.

What is the difference between Page.User and Context.User? And how can I get the same (expected) results inside Global.asax?

+1  A: 

At this stage, the context could still be running under the aspnet account or the account that you are running the app pool with. You may want to do a test where you create a couple (or all) of the events available in Global.asax and print out the result of Context.User.Name to a text file. Then maybe you will see if any value exists for the Name. What I've seen is that the Name will be blank if when the site is running as the aspnet account.

You may also want to check the connections made to you database and see who is trying to log into the database during the time that Context.User.IsInRole("Moderator") is returning false.

Also, (and this is the shot in the dark), you may want to see what Thread.CurrentPrincipal.IsInRole("Moderator") gives you. I hope these suggestions will help with debugging.

SideFX
Great suggestions! I found that indeed IsInRole() is returning different results in `PostAuthenticateRequest` then it is in `AuthorizeRequest` (authorizerequest seems to give the correct (expected) results).
Ropstah