【问题标题】:Data based on user role基于用户角色的数据
【发布时间】:2011-08-25 19:04:22
【问题描述】:

我的产品有多种价格。我希望根据用户组显示相应的价格。

到目前为止,我创建了描述这些组的角色

  1. 管理员
  2. 用户价格A
  3. 用户价格B
  4. 用户价格C

我想以某种方式获得当前用户角色。

类似这样的:

public decimal Price {
    get
    {
        var price = Prices.First(p => p.PriceType.Name == Something.CurentUser.Role);
        return price; 
    }
}

【问题讨论】:

    标签: asp.net-mvc-3 roles user-roles


    【解决方案1】:

    您可以根据每个角色的名称编写条件语句。根据用户是否是角色的成员,执行不同的 LINQ 语句。

    if (User.IsInRole("Admin")) {
        // ...
    }
    else if (User.IsInRole("UserPriceA")) {
       // ...
    }
    else if (User.IsInRole("UserPriceB")) {
       // ...
    }
    else if (User.IsInRole("UserPriceC")) {
       // ...
    }
    

    【讨论】:

      【解决方案2】:

      你不能这样做,因为一个用户可以有多个角色,但你可以这样做

      Prices.Where(p=> Membership.GetRoles().Contains(p.PriceType.Name))
      在这部手机上打字不好玩...抱歉简洁

      【讨论】:

        猜你喜欢
        • 2019-09-10
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 2020-10-24
        • 2015-04-21
        相关资源
        最近更新 更多