.NET MVC Forms 身份验证 - 调试 IsInRole()?
我在我的 MVC 网站上使用表单身份验证。我使用 Visual Studio 中的默认 ASP.NET 配置选项来管理用户和角色。到目前为止一切都很好。
我可以在视图中成功执行Page.User.IsInRole("Moderator")
。 True
按预期返回。
但是,当在 Global.asax 的 Application_PostAuthenticateRequest()
内调用 Context.User.IsInRole("Moderator")
时(或在循环,就此而言..)返回False
。
Page.User
和 Context.User
之间有什么区别?如何在 Global.asax 中获得相同的(预期)结果?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此阶段,上下文仍可能在 aspnet 帐户或运行应用程序池的帐户下运行。您可能想要进行一个测试,在其中创建 Global.asax 中可用的几个(或全部)事件并将 Context.User.Name 的结果打印到文本文件中。然后也许您会看到该名称是否存在任何值。我所看到的是,如果站点作为 aspnet 帐户运行,则名称将为空。
您可能还想检查与数据库建立的连接,并查看在 Context.User.IsInRole("Moderator") 返回 false 期间谁正在尝试登录数据库。
另外,(这是在黑暗中拍摄的),您可能想看看 Thread.CurrentPrincipal.IsInRole("Moderator") 为您提供了什么。我希望这些建议对调试有所帮助。
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.