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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:56:53+00:00 2026-05-25T19:56:53+00:00

I am working with MVC 3 and I have just implemented a wrapper for

  • 0

I am working with MVC 3 and I have just implemented a wrapper for the FormsAuthenticationService.

Something similar to the following.

public void SignIn(string username, bool createPersistantCookie)
{
    if (string.IsNullOrEmpty(username)) 
        throw new ArgumentException("Value Cannot be null or empty", "username");

    FormsAuthentication.SetAuthCookie(username, createPersistantCookie);
}

Reluctantly, I have gotten this to work, but now I am not quite sure how to get the information that I have stored.

Once the user is in my system, how can I now safely retrieve this information if I need to grab their UserID out of the database?

  • 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-25T19:56:54+00:00Added an answer on May 25, 2026 at 7:56 pm

    Based on the additional information provided, you want to store additional data with the FormsAuthentication ticket. To do so, you need first create a custom FormsAuthentication ticket:

    Storing Data

    Grab the current HttpContext (not worrying about testability)

    var httpContext = HttpContext.Current;
    

    Determine when the ticket should expire:

    var expires = isPersistent 
                    ? DateTime.Now.Add(FormsAuthentication.Timeout) 
                    : NoPersistenceExpiryDate; // NoPersistenceExpiryDate = DateTime.MinValue
    

    Create a new FormsAuthentication ticket to hold your custom data.

    var authenticationTicket = new FormsAuthenticationTicket(
                                 1, 
                                 username, 
                                 DateTime.Now, 
                                 DateTime.Now.Add(FormsAuthentication.Timeout), 
                                 isPersistent, 
                                 "My Custom Data String"); //Limit to about 1200 bytes max
    

    Create your HTTP cookie

    new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authenticationTicket))
      {
        Path = FormsAuthentication.FormsCookiePath,
        Domain = FormsAuthentication.CookieDomain,
        Secure = FormsAuthentication.RequireSSL,
        Expires = expires,
        HttpOnly = true
      };
    

    And finally add to the response

    httpContext.Response.Cookies.Add(cookie);
    

    Retrieving Data

    Then you can retrieve your data on subsequent requests by parsing the stored authentication ticket…

    Again, grab current HttpContext

    var httpContext = HttpContext.Current
    

    Check to see if the request has been authenticated (call in Application_AuthenticateRequest or OnAuthorize)

    if (!httpContext.Request.IsAuthenticated)
        return false;
    

    Check to see if you have a FormsAuthentication ticket available and that it has not expired:

    var formsCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (formsCookie == null)
      return false;
    

    Retrieve the FormsAuthentication ticket:

    var authenticationTicket = FormsAuthentication.Decrypt(formsCookie.Value);
    if (authenticationTicket.Expired)
      return false;
    

    And finally retrieve your data:

    var data = authenticationTicket.UserData;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working copy of asp.net mvc site locally. I just uploaded the
can anyone help? I have just started asp.net mvc and its all working.. entering
I have just started working in jQuery and ASP.NET MVC. I want the same
I'm working through a previous webforms application to convert it to MVC and have
I have only recently started working with the MVC approach, so I suppose this
I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
So I am working with ASP.Net MVC + jQuery autocomplete and I have a
I'm working my way through some ASP.NET MVC reading and I have a web
I'm working on a Spring MVC project, and I have unit tests for all
I am working with an ASP.NET MVC application. I have one master page having

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.