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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:02:08+00:00 2026-05-29T11:02:08+00:00

In our application we have implemented role-based forms authentication. This has been handled using

  • 0

In our application we have implemented role-based forms authentication. This has been handled using a RoleModule, where we save the Role data in cookie,
and each time we read the data from the cookie and instantiate the IPrincipal object. This code is executed in Application_OnPostAcquireRequestState method:

 HttpApplication application = source as HttpApplication;
 HttpContext context = application.Context;

if (context.User.Identity.IsAuthenticated &&
      (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
{
//Read the roles data from the Roles cookie..

context.User = new CustomPrincipal(context.User.Identity, cookieValue);
Thread.CurrentPrincipal = context.User;
}

This initializes the context.User object. Each time a request is made to the server, the user is authenticated using the above flow.
In the Application_EndRequest, we update the Roles cookie with the current principal object data.

There is FormsAuthentication_OnAuthenticate method in our Global.asax page, in which we read the cookie, update the cookie, and renew the
ticket if it is expired. Also, in this method, we try to set the username value in the session object if the ticket is expired.

FormsAuthentication oldTicket = FormsAuthentication.Decrypt(context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
if(oldTicket != null)
{
    if(oldTicket.Expired)
    {
        try
        {
            HttpContext.Current.Session["UserName"] = userName;
        }
        catch
        {
            //Log error.
        }

    FormsAuthentication newTicket =  new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, DateTime.Now,
            DateTime.Now.AddMinutes(30), oldTicket.IsPersistent, oldTicket.UserData);

    string encryptedTicket = FormsAuthentication.Encrypt(newTicket);
    HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    if (isPersistent)
    {
        httpCookie.Expires = DateTime.Now.AddMinutes(300);
    }
}

Here is our forms setting in web.config:

   <forms defaultUrl="Default.aspx" domain="" loginUrl="Login.aspx" name=".ASPXFORMSAUTH" timeout="20" slidingExpiration="true" />

The session timeout is 20 mins.

The issue: If a user is idle for more than 30 mins.(which is the FormsAuth ticket renewal time duration), the context.User.Identity.IsAuthenticated value in
the role module is false, and context.User is set as NULL. Now when a user requests a page he is redirected to the login page. However, the
cookie is still there. If again, the user tries to request a page, the context.User.IsAuthenticated property is set to true and the user is taken to the
respective page. Also, in FormsAuthentication_OnAuthenticate method, when I try to set the session value it throws an error as the session object is NULL.

What I want to achieve here is that in case of persistent cookie, after the auth ticket times out, the user should not be logged out, i.e. the user should
be re-authenticated.

How can I achieve this?
If I am not wrong, setting context.User should solve the purpose, but how do I go about it?

Additional Info:

After the ticket expires and i try to request a page, the event viewer shows an error message:

Event code: 4005 
Event message: Forms authentication failed for the request. Reason: The ticket supplied has expired. 
Event time: 08-02-2012 20:02:05 
Event time (UTC): 08-02-2012 14:32:05 
Event ID: 048e3238ade94fd6a7289bac36d130ef 
Event sequence: 76 
Event occurrence: 2 
Event detail code: 50202 

Process information: 
Process ID: 21692 
Process name: w3wp.exe 
Account name: IIS APPPOOL\ASP.NET v4.0 Classic 

I am using standard machine key settings, stored in web.config and not an auto-generated one. Also, I checked the process ID and its the same for all the errors.

  • 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-29T11:02:09+00:00Added an answer on May 29, 2026 at 11:02 am

    I finally resolved the issue. What was happening was that when the FormsAuthentication ticket timed out, FormsAuthentication_OnAuthenticate was not able to set the context.User object, as specified in the MSDN documentation for Authenticate event:

    If you do not specify a value for the User property during the
    FormsAuthentication_OnAuthenticate event, the identity supplied by the
    forms authentication ticket in the cookie or URL is used.

    The reason for that is that I am not setting ticket.Name with the username of the user. It is an empty string. Hence it might be the case that Authenticate event was not able to get the identity of the user and create the FormsIdentity instance. As a solution, when I am renewing the expired ticket, I am also creating a GenericIdentity object and then using it to set context.User.

    IIdentity identity = new GenericIdentity(username, "Forms");
    context.User = new CustomPrincipal(identity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our desktop application, we have implemented a simple search engine using an inverted
In many places in our application we have code like this: using(RAPI rapi =
We have recently implemented htmlpurifier in our web-based application. Earlier we used to have
I have a classic ASP application currently secured using ASP.NET Forms Authentication running on
In our application we have a little query that looks like this: var selectedAgents
We have J2EE application built using Hibernate and struts. We have RMI registry based
We have implemented JUnit4 in our application which uses Spring core & JPA with
We have a wicket application for our main website. Lately we have implemented a
I have implemented URL mapping in our ASP.NET 4 application, but I have a
in our application we have a Java applet running inside a .NET browser control.

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.