lexeme
lexeme

Reputation: 2973

Where to intercept user authentication

Ok. In my ASP.NET MVC application I'm using Windows Authentication. I want to use different themes (layouts) for user groups. So my question is where/how do I intercept user authentication to apply theming for the given group.

Should I handle it inside default route/action (and check whether request is authenticated?).

Thanks!

Upvotes: 1

Views: 355

Answers (1)

CD Smith
CD Smith

Reputation: 6607

We use a similar setup by creating multiple layouts, one for each role.

For example let's say you have an Accounting role so you create a layout page that references the Accounting.css file and any special .js files for that role

At the top of that layout use this

@{
    Layout = null;
}

And in your _ViewStart.cshtml partial view put something like this:

@{
    if (User.IsInRole("Admin")) {
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }
    else if(User.IsInRole("Accounting")) {
        Layout = "~/Views/Shared/_AccountingLayout.cshtml";
    }
    else if(User.IsInRole("HR")) {
        Layout = "~/Views/Shared/_HRLayout.cshtml";
    }
    else {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
}

I put the admin role at top because an admin typically has access to all the other roles as well, that is to say, in my case, an Admin use also belongs to all the other roles. So setting that at the top will immediately filter out any users who are not an admin and check to see their specific role, and any Admin is quickly getting the admin layout

Upvotes: 1

Related Questions