开发者

.NET MVC Forms authentication - debug IsInRole()?

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 m开发者_运维问答atter..) 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜