我在周末通过自定义身份验证实现了自定义RoleProvider。使用Silverlight业务模板,我可以在服务请求上放置一个[RequiresRole(“ Admin”)]属性:
[RequiresRole("Admin")]
public IQueryable<P_BUDGET> GetBudgets()
{
return this.ObjectContext.P_BUDGET;
}
这就像一个魅力。我用下面的代码
然后,我进入了Kyle McClellans授权库。如果我在XAML中的HyperlinkButton上设置了“ RequiresRole”属性(s:Authorization.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”方法,该方法已被我覆盖,并从我在数据库中拥有的View返回角色列表。第二个使用的是IsInRole,我读过的不是您应该修改的东西。
感谢您的帮助!
最佳答案
您的自定义角色提供者应负责从数据库中生成角色列表,或通过数据库调用验证用户是否在角色中
看一下以下来自Microsoft的示例代码:http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.isuserinrole.aspx
关于c# - IsInRole的问题-customRoleProvider,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5226775/