ASP .NET 自定义 RoleProvider 不尊重 cacheRolesInCookie=“true”
我已经实现了一个自定义角色提供程序,并在我的 web.config 文件中对其进行了如下配置:
<roleManager enabled="true" defaultProvider="TDRoleProvider" cacheRolesInCookie="true">
<providers>
<clear/>
<add name="TDRoleProvider" type="TDRoleProvider"/>
</providers>
</roleManager>
我已经覆盖了自定义角色提供程序中的 GetRolesForUser 函数,并且我已经进入其中,并且它工作得很好 - 加载我正在测试的用户有 60 个角色。但是,我注意到每个调用 User.IsInRole 的请求都会调用 GetRolesForUser。在我编写的其他应用程序中,它只调用一次,然后将结果缓存在 cookie 中。由于某种原因,缓存不适用于此应用程序。有什么想法吗?
I've implemented a custom role provider, and configured it in my web.config file like this:
<roleManager enabled="true" defaultProvider="TDRoleProvider" cacheRolesInCookie="true">
<providers>
<clear/>
<add name="TDRoleProvider" type="TDRoleProvider"/>
</providers>
</roleManager>
I've overridden the GetRolesForUser function in my custom role provider, and I've stepped into it, and it works just fine - loads up 60 roles for the user I'm testing with. However, I've noticed that the GetRolesForUser gets called on every request that calls User.IsInRole. In other apps I've written, it only calls it once, then caches the result in the cookie. For some reason, the caching is not working for this app. Any ideas as to why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
https://web.archive.org/web/20101123220352/http://connect.microsoft.com/VisualStudio/feedback/details/104688/rolemanager-cacherolesincookie-option-does-not-work< /a>
“RolePrincipal 中何时缓存(或不缓存)的问题经历了多次设计迭代,我们最终决定只缓存 IPrincipal 接口公开的方法(即 IsInRole)。”
https://web.archive.org/web/20101123220352/http://connect.microsoft.com/VisualStudio/feedback/details/104688/rolemanager-cacherolesincookie-option-does-not-work
"The issue of when to cache (or not cache) in RolePrincipal went through a number of design iterations, and we finally settled on only caching for the method exposed by the IPrincipal interface (i.e. IsInRole). "
我也遇到了同样的问题。就我而言,问题是我将 Context.User 设置为 GenericPrincipal 而不是 RolePrincipal。因此,而不是:
这对我来说是固定的:
IsValidAuthCookie方法检查 null 和空:
更新:升级到 MVC5 .NET 4.5 后,roleManager 停止工作(不在 cookie 中保存角色),因此必须自己保存:
保存roleCookie
将此代码放在AuthenticationFilter上并全局注册。请参阅此处。
I was having the same problem. In my case the issue was that I was setting Context.User to GenericPrincipal and not RolePrincipal. So instead of:
this fixed for me:
The IsValidAuthCookie method checks for null and empty:
UPDATE: After upgrading to MVC5 .NET 4.5 roleManager stopped working (not saving roles in cookie) so had to save it myself:
Save the roleCookie
Place this code on AuthenticationFilter and register it globally. See here.
对我来说也是如此。它不断调用 GetRolesForUser()
Same is true for me. It keeps calling GetRolesForUser()