【问题标题】:Extending .NET RoleProvider扩展 .NET RoleProvider
【发布时间】:2013-08-06 22:57:24
【问题描述】:

假设我的应用程序中有多个餐厅实体。每个餐厅都可以定义自己的经理、金融家、厨师等。

例子:

  • user1 是 restaurant1 的经理
  • user2 是 restaurant1 的金融家
  • user3 在 restaurant1 做饭

  • user1 是 restaurant2 的金融家

我想在 RoleProvider 中调用的是:IsUserInRole(user1, manager, restaurant1)。支持前 2 个参数,但不支持最后一个。

.NET RoleProvider 可以解决这种情况吗?

【问题讨论】:

    标签: .net roleprovider user-roles


    【解决方案1】:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多