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 6852011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:19:09+00:00 2026-05-27T01:19:09+00:00

I have override for controller that checks if certain session data exists. This data

  • 0

I have override for controller that checks if certain session data exists. This data is required for repository to work properly so if it does not exist then after the check the user should be logged off.

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    if (Session["CompanyID"] != null)
    {
        repo.CompanyID = (long)Session["CompanyID"];
    }
    else
    {
        RedirectToAction("LogOff", "Account");
    }
}

My code looks like this, but even when the RedirectToAction is invoked the controller still opens the default action and the user is not logged off.
Can You recommend on how to handle this problem?

I am using this session data in such a way because this is the first place i can get to it i know of and here i can check if this particular data exists. It is written when user logs in.

This data is a part of User in database. I have made a custom membership and roles provider. Is there a way to add this data to “User” of MembershipUser type somehow so it can be accessed in constructor like users username?

  • 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-27T01:19:09+00:00Added an answer on May 27, 2026 at 1:19 am

    Consider using a custom ActionFilter instead.

    public class HasCompanyIdAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["CompanyID"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {action = "LogOff", controller = "Account"}));
            }
        }
    }
    

    It can then be applied as so:

    [HasCompanyId]
    public class MyController : Controller 
    {
        public ActionResult SomeAction()
        {
            return View();
        }
    }
    

    This will apply the attribute for all requests that MyController (or it’s subclasses) handles.

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

Sidebar

Related Questions

I have a controller that overrides OnActionExecuting and does something like this: protected override
I have a base controller that I made so I can pass data to
I have one controller that takes a username and pass and checks against a
I have a fairly complex method in my controller that basically outputs data to
I have a certain part of my site that checks if the logged on
I have a data validation class that checks whether the start date of a
If I have a view (and its controller) and I override the willAnimateRotationToInterfaceOrientation method
I have a class that extends View. I override the onDraw method and allow
I have a function, a() , that I want to override, but also have
I have an ASP.NET MVC2 application that uses a parent controller to setup specific

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.