假设我的应用程序中有多个餐馆实体。每家餐厅都可以定义自己的经理、金融家、厨师等。
示例:
和
我想在RoleProvider中调用的是: IsUserInRole(user1,manager,restaurant1)。支持前两个参数,但不支持最后一个参数。
这个场景能由.NET RoleProvider解决吗?
发布于 2013-08-07 04:20:14
RoleProvider的IsUserInRole方法的语法是:
public abstract bool IsUserInRole(
string username,
string roleName
)因此,在重写时,不能包含第三个参数。
为什么不定义您自己的自定义方法,例如(额外的'S'):
IsUserInRoles(string username, string roleName1, string roleName2)或者更好的方法:
IsUserInRoles(string username, string[] roles)身体会喜欢:
protected bool IsUserInRoles(string username, string[] rolenames)
{
if (username == null || username == "")
throw exception;
if (rolenames == null || rolenames.Length==0)
throw exception;
//code to check if user exists in all roles
// you can call even the default IsUserInRole() method one by one for all roles
bool userInRoles=true;
foreach (string role in roles )
{
if( !UserIsInRole(role))
// set the boolean value to false
userInRoles = false;
}
return userInRoles;
}https://stackoverflow.com/questions/18091931
复制相似问题