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

  • Home
  • SEARCH
  • 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 5930829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:36:29+00:00 2026-05-22T14:36:29+00:00

Is there any good reason why ASP.NET’s session state cookie and the Forms Authentication

  • 0

Is there any good reason why ASP.NET’s session state cookie and the Forms Authentication cookie are two separate cookies? What if I want to “tie” them to each other? Is it possible in an elegant way?

Right now, I am stuck with the following solution, which works, but is still ugly:

[Authorize]
public ActionResult SomeAction(SomeModel model)
{
    // The following four lines must be included in *every* controller action
    // that requires the user to be authenticated, defeating the purpose of
    // having the Authorize attribute.
    if (SomeStaticClass.WasSessionStateLost/*?*/) {
        FormsAuthentication.SignOut();
        return RedirectToAction("Login", "Account");
    }

    // ...
}

@RPM1984: This is what happens:

[HttpPost]
public ActionResult Login(LoginModel loginModel)
{
    if (/* user ok */)
    {
        // ...
        Session["UserID"] = loginModel.UserID;
        Session["Password"] = loginModel.Password;
        // ...
    }
    else
    {
        return View();
    }
}

And it doesn’t take much guessing to know what WasSessionStateLost does.

  • 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-22T14:36:29+00:00Added an answer on May 22, 2026 at 2:36 pm

    I’ll start with a solution, then an explanation followed by a recommendation.

    Create a custom authorization attribute:

    Since your application defines Authorized as follows:

    • Logged in
    • Must have values in Session["UserID"] and Session["Password"]

    you need to define your own AuthorizationAttribute

        public class AuthorizedWithSessionAttribute : AuthorizeAttribute
        {    
            protected override bool AuthorizeCore(HttpContextBase httpContext)
            {
                if(httpContext.Request.IsAuthenticated && 
                    Session["UserID"] != null && Session["Password"] != null)
                    return true;
    
                // sign them out so they can log back in with the Password
                if(httpContext.Request.IsAuthenticated)
                    FormsAuthentication.SignOut(); 
    
                return false;
            }
        }
    

    Replace all your [Authorize] attributes with [AuthorizedWithSession] and you shouldn’t need to put session check code in your controllers.

    I don’t know enough about your application, but saving passwords in session (even worse in plain text) is not a secure thing to do.

    In addition, as RPM1984 said, the session cookie and authentication cookie are separate.

    Explanation:

    Think of the session as a bucket of info (on the server side) with your name on it. ASP.NET can take and put stuff in that bucket. ASP.NET gives you a name, your session id, and puts it on the bucket so it can know which one is yours.

    The authentication cookie tells ASP.NET that you’re authenticated and stores your authentication name in it. The authentication name is usually set by the developer of the application and is usually a unique key (think primary key in a DB) to separate you from the other users.

    Recommendation to be more secure:

    Encrypt the passwords before your store them. This is not total security, but it beats storing passwords in plain text and of course, if someone were to get a hold of the encryption key, they can crack the passwords.

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

Sidebar

Related Questions

Is there any good reason to disallow scriptlet or EL expression to be inserted
I just wondered whether there is any good reason for or even an advantage
I am using QTabWidget in my project. Is there any good reason I should
Are there any good reason to learn languages such as Ada and COBOL? Are
I have a couple classes extending builtin datetime.* Is there any good reason to
Is there any good reason why: std::string input; std::getline(std::cin, input); the getline call won't
is there any good reason not to use XML-RPC for an object-broker server/client architecture?
Are there any good reasons not to use \u0000 as a delimiter within a
Are there any good reasons for why you would include JavaScript like this: <script
Are there any good reasons to modify your project's build output path from its

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.