asp.net mvc3表单身份验证,是否可以设置多个AuthCookies?

时间:2012-07-23 15:20:47

标签: asp.net-mvc-3 asp.net-membership forms-authentication membership-provider

用户登录后。我将用户名保存到Cookie中。

FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

用户有角色,“成员”,“员工”,“经理”

对于那些只允许经理进入的页面。我现在所做的是使用他们的用户名查询数据库。

GetRoles(用户名)

这会产生很多开销,每次访问受角色保护的页面时,我都需要检查数据库。

我想知道我是否可以让SetAutheCookie设置一些值,而不仅仅是用户名。

我现在所做的是将用户名和角色组合在一个字符串中。

FormsAuthentication.SetAuthCookie(userName +“|”+ role.tostring(),createPersistentCookie);

这绝对不是最佳解决方案。

1 个答案:

答案 0 :(得分:1)

对此SO问题的最热烈回答提供了在表单身份验证Cookie中存储和检索角色的示例:Store/assign roles of authenticated users