ASP.NET 4.5/C#/ SQL 2012另一个ASP.NET自定义成员资格/角色提供
我已经有一个用户表和角色/权限列一个定义良好的数据库。该列包含角色的分隔列表。我希望在后端使用ASP.NET的内置功能,所以我可以懒惰,并使用诸如按角色筛选和按角色筛选。我想从数据库中读取我的角色,并告诉ASP这些是我的用户所在的角色。自定义...但希望很简单。以下是我迄今为止:
//create an identity
GenericIdentity objIdentity = new GenericIdentity("Matt"); //this would actually be the username/email of the newly authenticated user
//get roles for user
string[] strRoles = { "ADW", "USR" }; //this will get read from the database on authentication
//add identity and roles to a principal
GenericPrincipal objPrincipal = new GenericPrincipal(objIdentity, strRoles);
//add to current user
HttpContext.Current.User = objPrincipal;
//add the principal to the current context of the current thread
Thread.CurrentPrincipal = objPrincipal; //not sure what this does, doesn't affect my results/tests
如果我执行上面的代码,然后运行以下命令:
Response.Write(User.IsInRole("ADW"));
Response.Write(User.IsInRole("xxx"));
我得到一个真正的/预期假。但是,这不会持续到下一页。我对定制的成员/角色提供者做了大量的阅读,但是我找不到这个具体的用例。大多数人都在谈论为此任务专门设置数据库。我也看到提到了更新的简单会员,但是也没有从这方面得到任何的爱。我希望有一个解决方案涉及我已经在做的事情。我很想在用户验证时执行此代码,但能够在其他页面中引用此用户。我可以随时调用数据库来查看每个页面。我假设会吸引人,而这不是内置提供者所做的。
谢谢大家。
我不完全明白。我试图利用诸如读取角色的LoginView和web.config权限。我想从代码隐藏中为当前用户添加一个角色。 – Matt
这是RoleProvider用于读取角色并将其存储到数据库的内容。为了登录用户,必须使用表单cookie。 –
编辑我的答案。仍然不确定你错过了哪部分。 –