IsInRole仅在用户登录时起作用

Cos*_*sta 1 forms-authentication asp.net-membership asp.net-roles

为什么HttpContext.Current.User.IsInRole("Customer")如果用户没有登录则返回false,我认为因为用户被认为是匿名的,这种情况,正确吗?

谢谢

Sky*_*ers 5

是的,你是对的.匿名用户不能属于某个角色.

请注意,User匿名用户的对象是a GenericPrincipal,当它实现时IPrincipal,该IsUserInRole方法始终返回false,因为创建它时没有提供角色FormsAuthenticationModule.

还要注意,User为已认证用户的对象是RolePrincipal,它查询RoleManagerIsUserInRole.