开发者

IsInRole Works only when user sign in

开发者 https://www.devze.com 2022-12-30 11:42 出处:网络
Why HttpContext.Current.User.IsInRole(\"Customer\") returns false if the user is not logged in, I t开发者_开发技巧hink because user is considered anonymous is this case, Correct?

Why HttpContext.Current.User.IsInRole("Customer") returns false if the user is not logged in, I t开发者_开发技巧hink because user is considered anonymous is this case, Correct?

Thanks


Yes, you are correct. Anonymous users cannot belong to a role.

Notice that the User object for an anonymous user is a GenericPrincipal and while it implements IPrincipal, the IsUserInRole method always returns false because there are no roles supplied when it is created by FormsAuthenticationModule.

Also notice that the User object for an authenticated user is a RolePrincipal, which queries RoleManager in IsUserInRole.


Until the user don't login to your application, application behave it as an anonymous user and this user is no known for your application.


This is the default behavior, the anonymous user has no roles.

But if it returns false you can't be sure the user is not authenticated (better use User.Identity.IsAuthenticated property).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号