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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:36:35+00:00 2026-06-12T21:36:35+00:00

This will be my first question here! Im having problems with my mvc4 app

  • 0

This will be my first question here!

Im having problems with my mvc4 app and random occurring logouts.

i use sessions to store my company id and id of the user.

        private void SetSessionData(string UserName)
    {
        Employee data = (from employee in _db.Employees where employee.Email == UserName select employee).First();
        Session.Add("Comp_ID", data.Comp_ID);
        Session.Add("Company", data.Company.Name);
        Session.Add("User_ID", data.ID);
    }

i have set the timeout value to 600 for the session (10 hours) this is even set 2 places to be sure:

        [AllowAnonymous]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
            //FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); //sørger for at remember me virker!

            SetSessionData(model.UserName);
            Session.Timeout = 600;

            if (model.RememberMe)
            {
                Response.Cookies.Add(new HttpCookie("CookieUserName", model.UserName) { Expires = DateTime.Now.AddDays(30), Value = model.UserName });
                Response.Cookies.Add(new HttpCookie("CookieRememberMe", model.RememberMe.ToString()) { Expires = DateTime.Now.AddDays(30), Value = model.RememberMe.ToString() });//sætter den nye cookie
            }
            else
            {
                Response.Cookies.Set(new HttpCookie("CookieUserName") { Expires = DateTime.Now.AddDays(-1) });
                Response.Cookies.Set(new HttpCookie("CookieRememberMe") { Expires = DateTime.Now.AddDays(-1) });
            }

            if (string.IsNullOrEmpty(returnUrl))
            {
                return RedirectToLocal(returnUrl);
            }
            return RedirectToAction("Index", "Home");
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "Vi har enten ikke brugernavnet eller koden i kartoteket.");
        return View(model);
    }

and here in the web.config:

<system.web>
<machineKey validationKey="MyKeyGoesHere" validation="SHA1" decryption="AES" />
<sessionState timeout="600" />
<compilation debug="true" targetFramework="4.5">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="600" />
</authentication>

My cookies seem to be saved for 10 hours, and my session_id cookie expiration seems to be set to “when the browser closes”.

Server side i have set the app pool to recycle at 1am.

Even though all this is set my users still get random logouts form everything between 2 min after login to 1 hour after login.

to counter some of the random half login state problems i have had i included this:

@if(Session == null || Session["User_ID"] == null || !WebSecurity.Initialized){
                //Makes sure the session data is cleared and user logged out if session dies.
                try
                {
                    if(Session != null) {Session.Clear();}

                    if (WebSecurity.Initialized){WebSecurity.Logout();}

                    FormsAuthentication.SignOut();

                    //dette er til at stoppe cache.
                    Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Cache.SetNoStore();

                }catch{ <p>Error Clearing Login Cache</p>}    
            }

Im pretty lost by now and hopes a guru out there might know what beginners mistake im making here!

Thanks for ALL response in advance!

Edit:

I also tried this: http://www.windowsitpro.com/article/security-development/create-persistent-id-cookies

(original link from: ASP.NET MVC FormsAuthentication Cookie timeout cannot be increased)

but that just made my app logout every single time i pressed anything after login.

The app is running on windows 2012 server with IIS8.

More adds:

I found out the session_id cookie is still set to when closed in the browser:

cloud.hviidnet.com/image/2X3v2y2e1K1S

The strange thing is its set to 600 min, even when i look in the IIS server:

cloud.hviidnet.com/image/1e3J1g2u3p2M

  • 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-12T21:36:37+00:00Added an answer on June 12, 2026 at 9:36 pm

    The solution was to remove all use of “Session.” and get all the data from the database instead with WebSecurity.CurrentUserID.

    Hope this helps someone else!

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

Sidebar

Related Questions

This will be my first iOS app with any bit of complexity. I'd like
I know one awkward solution for this taks will be : first use ct
I'm posting my first question here after having viewed many useful exchanges by others;
I'm having some trouble with this problem in Project Euler. Here's what the question
This is my first question here. I have been searching Stack Overflow and other
This is my first post in here and I wouldn't post a question here
I'm fairly new to writing test cases and this will be my first major
This is the first time I have attempted to create a .dll, that will
This is my first time on this site. I will ask you to help
This will be probable quite odd question. But i thought I will give it

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.