-1

We faced following problem. We developed set of post-login pages for our system. And users could have one of 5 roles: Free User Type 1, Premium User Type 1, Free User Type 2, Premium User Type 2 and Admin. The problem is that even though all pages for every of these roles should look almost the same but it it is still a bit different depending on user role (for example links point to different URLs, or different modal are shown when buttons are clicked, some options are shown and some are hidden). What we are trying to do is to wrap this all in small partial views and render different partial views depending on user role. But it gets more and more complex. Maybe there is some kind of design pattern or general approach to deal with this problem? Thanks!

Stanislav
  • 165
  • 1
  • 15
  • You could implement a [custom razor view engine](http://stackoverflow.com/questions/9838766/how-do-i-implement-a-custom-razorviewengine-to-find-views-in-non-standard-locati). This could contain your user switching logic. – Liam May 19 '16 at 09:55

1 Answers1

1

Seems based on your description which is not 100% clear that the best approach would be to create the different partial views for the different roles.

Then on login get the user role from the DB, and return the different partial views based on the role.

If you have multiple partial views i.e. more than one page per user role, you can add the user role to a session or a cookie so you do not have to hit the DB again.

Id recommend using the cookie approach if this is the case.

Set Session

var userRole = 1;

Session["UserRole"] = userRole;

Get Session:

var userRole = Session["UserRole"] as int?;

Set Cookie

 var cookie = new HttpCookie("UserRole");
            cookie.Value = GetUserRole();
            cookie.Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies.Add(cookie);

Get Cookie

if (HttpContext.Current.Request.Cookies["UserRole"] != null)
{
    var userRole = HttpContext.Current.Request.Cookies["UserRole"].Value);
}
David
  • 641
  • 1
  • 8
  • 23