Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:29:04+00:00 2026-05-21T23:29:04+00:00

My MVC 3 webapp has different parts which require to check whether the user

  • 0

My MVC 3 webapp has different parts which require to check whether the user is a user or an admin, they all get access to the same pages, except some pages have controllers (buttons and textboxes) that only admins can see. I do that check by putting the user’s access level into my viewmodel and doing a check:

@if (Model.UserAccess != "Viewer")
{
    [do something]
}

I check in my action what access the logged in user has. If the session were to timeout I redirect them to the logon page.

My action is called from a Project page view and loaded as a partial:

@{Html.RenderAction("CategoryList", "Home", new { categoryId = Model.CategoryId });}

And my Controller:

public PartialViewResult CategoryList(int categoryid = 0)
{
    var useraccess = GetUseraccess();

    [blablabla stuff done here]

    var model = new CategoryViewModel()
    {
        CategoryList = categorylist
        UserAccess = useraccess
    };

    return PartialView(model);
}

public string GetUseraccess()
{
    var useraccess = "viewer"; //default

    var check = SessionCheck();
    if (check == "expired")
    {
        ModelState.AddModelError("", "Session expired. Please login again.");

        Response.Redirect("/Account/LogOn");
    }
    else if (check == "admin")
    {
        useraccess = "admin";
    }

    return useraccess;
}

public string SessionCheck()
{
    if (Session["UserAccess"] == null)
    {
        return "expired";
    }
    else if ((string)Session["UserAccess"] == "admin")
    {
        return "admin";
    }
    else // viewer
    {
        return "viewer";
    }
}

Now that works fine. However I’ve been trying to implement a custom attribute that would check the session’s expiration before the controller is fired:

public class CheckUserAccessSessionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var useraccess = filterContext.HttpContext.Session["UserAccess"];
        if ((string)useraccess == null)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Account", action = "LogOn" }));
        }
    }
}

[CheckUserAccessSession]
public PartialViewResult CategoryList(int categoryid = 0)
{
    [same stuff as above here]
}

I get the error

Exception Details: System.InvalidOperationException: Child actions are not allowed to perform redirect actions.

I understand why the error happens. But I haven’t found how to go around it. Similarly I’d like to send to my action some data from another custom attribute but that also isn’t working because of the RedirectToRouteResult.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:29:04+00:00Added an answer on May 21, 2026 at 11:29 pm

    Your issue here that is you’re returning a PartialViewResult on that action method, and by definition the output of that action method is going to be only a portion of the full request served by IIS. You should be doing permissions checking on the action method that serves the full view in which the partial view is incorporated.

    Technically when you’re calling Response.Redirect in your initial implementation you’re breaking far away from the ASP.NET MVC design conventions; even though it works, it’s bad design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small Spring MVC webapp (which embeds ActiveMQ) that is designed to
Let's say I have an ASP.NET MVC webapp that calls a repository layer, which
I'm building a questionnaire mvc webapp, and i cant figure out how to pass
aspnet mvc has the HandleError filter that will return a view if an error
Using MVC with an observer pattern, if a user action requires polling a device
Which MVC-framework is the best option (performance/ease of development) for a web application, that
ASP.NET MVC has been discussed on this forum a few times. I'm about to
Using ASP.NET MVC there are situations (such as form submission) that may require a
Our solution file contains many different projects including an ASP.NET MVC web app, a
I've got an Asp.Net mvc web app that has a controller action to handle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.