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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:15:52+00:00 2026-05-31T05:15:52+00:00

I have taken code from a previous custom Authorization attribute and come up with

  • 0

I have taken code from a previous custom Authorization attribute and come up with this:

    public class PortalAuthorizeAttribute : AuthorizeAttribute
{
    private WebSiteSession m_UserSession;
    protected WebSiteSession myUserSession
    {
        get
        {
            if (m_UserSession == null)
                try { m_UserSession = (WebSiteSession)HttpContext.Current.Session["UserSession"]; }
                catch
                {
                    m_UserSession = new WebSiteSession();
                    HttpContext.Current.Session["UserSession"] = m_UserSession;
                }
            return m_UserSession;
        }
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.Result is HttpUnauthorizedResult || myUserSession == null || !myUserSession.IsAuthenticated || myUserSession.AdvertiserId == 0)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
            {
                    { "client", filterContext.RouteData.Values["client"] },
                    { "controller", "Account" },
                    { "action", "Login" },
                    { "returnUrl", filterContext.HttpContext.Request.RawUrl }
            });
        }
    }
}

I am working from within a larger project so we get our session from a higher namespace. The issue it will redirect fine if I am logged out or if I do not have authorization (redirect to the area login page) but it seems if I idle (session times out??) it will still act like I am authorized but will not have any credentials attached to the session. So it still thinks I am validly logged in but I am not. Am I forgetting something in my filter check? The cookie is no longer valid but its like the user is still able to access the page.

Thanks

  • 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-31T05:15:53+00:00Added an answer on May 31, 2026 at 5:15 am

    The problem with your code stems from the fact that ASP.NET MVC 3 might cache instances of action filters. It is one of the breaking changes:

    In previous versions of ASP.NET MVC, action filters are create per
    request except in a few cases. This behavior was never a guaranteed
    behavior but merely an implementation detail and the contract for
    filters was to consider them stateless. In ASP.NET MVC 3, filters are
    cached more aggressively. Therefore, any custom action filters which
    improperly store instance state might be broken.

    And since you have cached the m_UserSession private field into the action filter, it’s never null and your test doesn’t pass.

    So, here’s how you could proceed:

    public class PortalAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var userSession = filterContext.HttpContext.Session["UserSession"] as WebSiteSession;
            if (userSession == null || !userSession.IsAuthenticated || userSession.AdvertiserId == 0)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
                {
                    { "client", filterContext.RouteData.Values["client"] },
                    { "controller", "Account" },
                    { "action", "Login" },
                    { "returnUrl", filterContext.HttpContext.Request.RawUrl }
                });
            }
        }
    }
    

    As far as creating and storing a new WebSiteSession instance into the session, that’s not something that an authorization filter should do. It’s something that you should do inside your Login action upon successful authentication. The authorization filter is there to only check if the user is authorized to access the action.

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

Sidebar

Related Questions

I have taken over some code from a previous developer and have come across
I have no idea why this isn't working. I've taken code from a previous
This code is taken from a previous question, but my question directly relates to
I have the following piece of code taken from the PHP manual on the
So I have this code that takes care of command acknowledgment from remote computers,
I have to take over and improve/finish some code that transforms Java objects from
I have a piece of code that takes several rows from a database and
Does anyone have some code that will take a TimeZoneInfo field from .NET and
I have taken over a large code base and would like to get an
We have taken over some .NET 1.1 Windows Service code that spawns threads to

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.