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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:03:41+00:00 2026-05-19T04:03:41+00:00

i’m working on a custom login page in mvc.net. I check logins like so:

  • 0

i’m working on a custom login page in mvc.net. I check logins like so:

public bool Login(string login, string password, bool persistent)
{
  var loginEntity = this.AdminRepository.GetLogin(login, password);
  if (loginEntity != null)
  {
    FormsAuthentication.SetAuthCookie(login, persistent);

    HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
    HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;

  return true;
  }

then i decorate any controller that needs admin access with a filter attribute:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  var ctx = HttpContext.Current;

  // check if session is supported
  if (ctx.Session != null)
  {
    var redirectTargetDictionary = new RouteValueDictionary();

    // check if a new session id was generated
    if (ctx.Session.IsNewSession)
    {
        // If it says it is a new session, but an existing cookie exists, then it must
        // have timed out
        string sessionCookie = ctx.Request.Headers["Cookie"];
        if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
        {
          redirectTargetDictionary = new RouteValueDictionary();
          redirectTargetDictionary.Add("area", "Admin");
          redirectTargetDictionary.Add("action", "LogOn");
          redirectTargetDictionary.Add("controller", "Home");

          filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
        }
      } else if (SessionContext.AdminId == null) {
        redirectTargetDictionary = new RouteValueDictionary();
        redirectTargetDictionary.Add("area", "Admin");
        redirectTargetDictionary.Add("action", "LogOn");
        redirectTargetDictionary.Add("controller", "Home");

        filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
      }
    }
    base.OnActionExecuting(filterContext);
}

I see that after log in I have two cookies:

  1. ASPXAUTH (with expiration date set
    to “At end of session” (when
    persists is false) OR (30 min from
    now (when persists is set to true)
  2. and ASP.NET_SessionId which
    expiration time is always “At end of
    session”

Question:
The problem is that even though i set TRUE to “persists” option (which will set ASPXAUTH expiration time 30 min from now -which is good) my Session[“AdminId”] is always null after i close and reopen the browser. How do i make sure my Sessions (Session[“AdminId”] and Session[“AdminUsername”]) are pulled in from the cookie when I initially do set “persists” to true and close then re-open the browswer window.
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-19T04:03:41+00:00Added an answer on May 19, 2026 at 4:03 am

    I found my solution here:Is it possible to use .ASPXAUTH for my own logging system?

    and this is what i did:

        public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Controller action filter is used to check whether the session is still active. If the session has expired filter redirects to the login screen.
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var ctx = HttpContext.Current;
    
            // check if session is supported
            if (ctx.Session != null)
            {
                // check if a new session id was generated
                if (ctx.Session.IsNewSession)
                {
                    var identity = ctx.User.Identity;
    
                    // If it says it is a new session, but an existing cookie exists, then it must
                    // have timed out
                    string sessionCookie = ctx.Request.Headers["Cookie"];
                    if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
                    {
                        var redirectTargetDictionary = new RouteValueDictionary();
                        redirectTargetDictionary.Add("area", string.Empty);
                        redirectTargetDictionary.Add("action", "LogOn");
                        redirectTargetDictionary.Add("controller", "User");
    
                        filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                    }
    
                    // Authenticated user, load session info
                    else if (identity.IsAuthenticated)
                    {
                        var loginRepository = new LoginRepository(InversionOfControl.Container.Resolve<IDbContext>());
                        IAuthenticationService authenticationService = new AuthenticationService(loginRepository);
                        authenticationService.SetLoginSession(identity.Name);
                    }
                }
                else if (SessionContext.LoginId == null)
                {
                    var redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("area", string.Empty);
                    redirectTargetDictionary.Add("action", "LogOn");
                    redirectTargetDictionary.Add("controller", "User");
    
                    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And

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.