上周末,我使用自定义身份验证实现了一个自定义RoleProvider。使用Silverlight Business模板,我能够在服务请求上添加一个RequiresRole("Admin")属性:
[RequiresRole("Admin")]
public IQueryable<P_BUDGET> GetBudgets()
{
return this.ObjectContext.P_BUDGET;
}这就像一个护身符。我使用了以下代码
然后我顺道访问了Kyle授权库。如果我在我的XAML (s:Authorization.RequiresRole="Admin")中的HyperlinkButton上设置了"RequiresRole“属性,它会在应用程序加载时成功地隐藏按钮。当我登录时,我希望它能识别我的测试用户所在的"Admin“角色,最终将该HLB的可见性更改为true。但是,当我单步执行代码时,我进入了我的App.Web.g.cs文件,它具有以下功能:
public bool IsInRole(string role)
{
if ((this.Roles == null))
{
return false;
}
return global::System.Linq.Enumerable.Contains(this.Roles, role);
}在上面的代码中,this.Roles为空。这里我漏掉了什么?第一个代码块使用了我已经覆盖的"GetRolesForUser“方法,并从我的数据库中的一个视图返回一个角色列表。第二个使用的是IsInRole,我已经读过了,你不应该修改它。
感谢您的帮助!
发布于 2011-03-09 02:55:35
谢谢你们的回答,但是我通过Kyle McClellan在他自己的回答中的评论找到了解决方案here。我已经覆盖了GetAuthenticatedUser,其中包括从数据库获取我的角色。非常简单,我必须放入user.Roles = roles,其中roles是从我的db视图返回的角色的列表。
发布于 2011-03-08 12:38:54
您的自定义角色提供程序应负责从数据库生成角色列表,或通过数据库调用验证用户是否属于某个角色
看一下来自微软的示例代码:http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.isuserinrole.aspx
发布于 2011-03-08 07:59:38
我已经实现了我的custon角色提供程序,您需要在global.asax,int Application_Start方法中创建角色。我有这样的东西:
void Application_Start(object sender, EventArgs e)
{
Roles.ApplicationName = "MyAppName";
if (!Roles.RoleExists("admin"))
Roles.CreateRole("admin");
if (!Roles.RoleExists("operator"))
Roles.CreateRole("operator");
if (!Roles.RoleExists("user"))
Roles.CreateRole("user");
}祝好运。
https://stackoverflow.com/questions/5226775
复制相似问题