我有一个自定义角色提供程序,它从数据库获取用户所属的角色。我还在 web.config 的 httpModules 中注册了一个自定义身份验证模块,它会嗅探传入的 HTTP 请求,并(如果是 OAuth 签名请求)设置 HttpContext.Current.User 属性来模拟用户,并且它设置的 IPrincipal 包括所有用户的角色,加上一个称为“委派”的额外角色。
问题是,在我设置自定义 IPrincipal 后,显然 ASP.NET 仍然调用我的自定义角色提供程序,然后使用仅具有该用户的标准角色的提供程序重置 IPrincipal。
如果我在 web.config 文件中设置
,则身份验证模块分配的角色会保留。但显然,我想要两全其美。如何使用角色提供程序,但在身份验证模块决定时“取消”角色提供程序的效果?
I have an custom role provider that gets the roles a user belongs to from a database. I also have a custom authentication module registered in my web.config's httpModules which sniffs incoming HTTP requests and (if it's an OAuth signed request) sets the HttpContext.Current.User property to impersonate the user, and the IPrincipal that it sets includes all the user's roles, plus an extra one called "delegated".
The trouble is, after I set my custom IPrincipal, apparently ASP.NET still calls my custom role provider, and then resets the IPrincipal with one that has only the standard roles for that user.
If I set <roleManager enabled="false" ...>
in my web.config file, the authentication module's assigned roles stick. Obviously though, I want the best of both worlds. How can I use the role provider, but "cancel" the role provider's effect when my authentication module decides to?
发布评论
评论(1)
事实证明,在身份验证 http 模块的 Init 方法中,我可以找到 RoleManager,然后挂钩一个事件,该事件使我能够否决它是否执行其重写工作:
It turns out that in the authentication http module's Init method, I can find the
RoleManager
, and then hook an event that gives me veto power on whether it does its overriding work: