Search code examples
asp.net-mvc-3rolesuser-roles

Data based on user role


I have a product with array of prices. I want that based on the user group the corresponding price will be displayed.

As far i created roles that are describing these groups

  1. Admin
  2. UserPriceA
  3. UserPriceB
  4. UserPriceC

I would like somehow to get current users role.

Something like this:

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

Solution

  • You can write a conditional statement based on the name of each role. Based on if the user is a member of the role, execute a different LINQ statement.

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