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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:41:12+00:00 2026-06-04T17:41:12+00:00

I have an MVC3 app and the user needs to be authenticated before they

  • 0

I have an MVC3 app and the user needs to be authenticated before they can access the site. I was logged in and was on a screen showing data, then i left. I came back an hour later, and by now the user has been automatically logged off. But when i clicked on a button to get data, without logging back in, I got the yellow screen of death cause my object was null. This is my Action:

  [Authorize]
    public ActionResult Index(string id)

I thought the [Authorize] attribute made sure to not execute this action unless they are authenticated, but apparently it doesnt or im not using it properly. So how do i use the [Authorize] or any other attribute to make sure the user is always authenticated and if they’re not, take them to the login screen? Thanks

P.S. This only happens when a timeout has occurred. If i just try to access a view by typing in the URL and not logging in, i get the login screen as expected

  • 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-06-04T17:41:13+00:00Added an answer on June 4, 2026 at 5:41 pm

    I came back an hour later, and by now the user has been automatically logged off

    If the action is executed, this means that the user is not logged off. The [Authorize] attribute works as expected.

    I suspect that your problem has nothing to do with authentication. It has to do with ASP.NET Session that you are using. Please bear in mind that Forms Authentication cookie and ASP.NET Session are 2 completely different notions. So I guess that you have stored something into the ASP.NET Session when the user logged in. Except that the ASP.NET Session by default expires in 20 minutes and is configured independently of the forms authentication cookie timeout. So while the user authentication cookie is still valid and the user is authenticated it has lost its session because either the session expired or the web server simply recycled your application and everything you stored into the Session is of course lost.

    This being said let’s see the different possibilities that you have to workaround this problem:

    1. My recommended approach is to get rid of the Session. Simply put <sessionState mode="Off" /> in your web.config and forget about the statefulness that the ASP.NET Session introduces in stateless applications.
    2. Write a custom Authorize attribute which in addition to checking whether the authentication cookie is valid it will check if the Session still contains values:

      public class MyAuthorizeAttribute : AuthorizeAttribute
      {
          protected override bool AuthorizeCore(HttpContextBase httpContext)
          {
              var authorized = base.AuthorizeCore(httpContext);
              if (!authorized)
              {
                  return false;
              }
      
              return httpContext.Session["someKeyThatYouHaveStored"] != null;
          }
      }
      

    and then use this custom attribute:

    [MyAuthorize]
    public ActionResult Index(string id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I have created a simple MVC3 web site and I want to limit access
In an ASP.NET MVC3 app I have a model that represents a user address
I have an MVC3/EF4.1 app that I'm building which now works fine on my
I'm developing a ASP.Net MVC3 app which will have few hundred videos. I want
I have an MVC3 C#.Net web app. I have a requirement to display a
I have an MVC3 C# .Net web app. The app works fine when launching
I have a MVC3 project for running on the iPad in the web-app mode.
I have a C# .Net web app using MVC3. I am using Aspose to
I have an MVC3 C#.Net web app. I am needing to zip a subset

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.