在以下简单场景中,Roles.IsUserInRole 的行为是否符合预期?
在 .NET 2.0 中的自定义角色提供程序(继承自 RoleProvider)中,IsUserInRole 方法已被硬编码为始终返回 true:
public override bool IsUserInRole(string username, string roleName) { return true; }
在配置为使用此角色提供程序的 ASP.NET 应用程序中,以下代码返回 true(如预期) :
Roles.IsUserInRole("any username", "any rolename"); // results in true
但是,以下代码返回 false:
Roles.IsUserInRole("any rolename"); // results in false
请注意,User.IsInRole("any rolename") 也返回 false。
- 这是预期的行为吗?
- 假设仅采用角色名称的重载仍会调用重写的 IsUserInRole 是否不正确?
更新:请注意,对于采用单个字符串的版本似乎没有可用的覆盖,这导致了我在 #2 中的假设。
In a custom role provider (inheriting from RoleProvider) in .NET 2.0, the IsUserInRole method has been hard-coded to always return true:
public override bool IsUserInRole(string username, string roleName) { return true; }
In an ASP.NET application configured to use this role provider, the following code returns true (as expected):
Roles.IsUserInRole("any username", "any rolename"); // results in true
However, the following code returns false:
Roles.IsUserInRole("any rolename"); // results in false
Note that User.IsInRole("any rolename") is also returning false.
- Is this the expected behavior?
- Is it incorrect to assume that the overload that only takes a role name would still be invoking the overridden IsUserInRole?
Update: Note that there doesn't seem to be an override available for the version that takes a single string, which has led to my assumption in #2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我查看了 .net 反射器中的 Roles.IsUserInRole(string rolename),它解析为以下内容:
我将查看您当前的用户。 原因如下:
我愿意打赌这会返回一个空字符串,因为您要么没有当前用户,要么它的名称是空字符串或 null。
在
IsUserInRole(string username, string roleName)
方法中,在开头附近有以下代码块:如果您的
GetCurrentUserName()
没有返回任何有意义的内容,那么它会在调用您重写的方法之前返回 false。从这一点可以看出:Reflector 是一个很棒的工具:)
I looked at Roles.IsUserInRole(string rolename) in .net reflector, and it resolves to the following:
I would take a look at your current user. Here's why:
I would be willing to bet this is returning an empty string because you either don't have a Current User, or its name is an empty string or null.
In the
IsUserInRole(string username, string roleName)
method, there is the following block of code right near the beginning:If your
GetCurrentUserName()
doesn't return anything meaningful, then it will return false before it calls your overridden method.Moral to take away from this: Reflector is a great tool :)
另请注意,您是否在 RoleManager 配置中选择了 cacheRolesInCookie="true"。 如果您向数据库添加了新角色,它可能会查看 cookie 中的缓存版本。
我遇到了这个问题,解决办法是删除cookie并重新登录。
Also beware if you have selected cacheRolesInCookie="true" in the RoleManager config. If you have added a new role to the database, it might be looking at the cached version in the cookie.
I had this problem and the solution was to delete the cookie and re-login.
这可能会对某人有所帮助 - 请注意:
如果您使用登录控件进行身份验证 - 输入到控件中的用户名将成为 HttpContext.Current.User.Identity.Name,用于 Roles.IsUserInRole(string rolename) ,更具体地说- 会员资格的 GetUser() 方法。 因此,如果是这种情况,请确保您重写 Authenticate 事件,在此方法中验证用户并将用户名设置为您的自定义会员资格提供程序可以使用的值。
This may help someone - be aware:
If you are using the login control to authenticate - the username entered into the control becomes the HttpContext.Current.User.Identity.Name which is used in the Roles.IsUserInRole(string rolename) and more specifically - the membership's GetUser() method. So if this is the case make sure you override the Authenticate event, validate the user in this method and set the username to a value that your custom membership provider can use.