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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:50:28+00:00 2026-05-12T16:50:28+00:00

I’m working on an application using ASP.NET MVC 1.0 and I’m trying to inject

  • 0

I’m working on an application using ASP.NET MVC 1.0 and I’m trying to inject a custom IPrincipal object in to the HttpContext.Current.User object.

With a traditional WebForms application I’ve used the Application_AuthenticateRequest event to do this as follows.

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    // Get Forms Identity From Current User
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    // Get Forms Ticket From Identity object
                    FormsAuthenticationTicket ticket = id.Ticket;
                    // Create a new Generic Principal Instance and assign to Current User
                    SiteUser siteUser = new SiteUser(Convert.ToInt32(id.Name));

                    HttpContext.Current.User = siteUser;
                }
            }
        }

    }

So using this I was able to access my custom IPrincipal by either explicitly casting the User object to type SiteUser. I actually did this by having a custom class that all Pages were inheriting from which did this under the covers for me.

Anyhow, my problem is that with ASP.NET MVC the Application_AuthenticateRequest seems to fire whenever any request is made (so for JS files, images etc.) which causes the application to die.

Any help or suggestions as to how I can go about injecting my custom IPrincipal in to the HttpContext.Current.User object within ASP.NET MVC 1.0 would be greatly appreciated. I did see the following post on SO, but it didn’t seem to cater for what I’m trying to achieve: ASP.NET MVC – Set custom IIdentity or IPrincipal

TIA.

  • 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-12T16:50:28+00:00Added an answer on May 12, 2026 at 4:50 pm

    my problem is that with ASP.NET MVC
    the Application_AuthenticateRequest
    seems to fire whenever any request is
    made (so for JS files, images etc.)
    which causes the application to die.

    This isn’t an uniquely MVC problem – if you ran your application on IIS7 with the integrated pipeline in place then you would see the same thing.

    If the problem with the lookup is scalability then I assume the actual problem is within

    FormsAuthenticationTicket ticket = id.Ticket;
    SiteUser siteUser = new SiteUser(Convert.ToInt32(id.Name));
    

    I’d guess that your SiteUser class does some sort of database lookup. If you examine how forms auth works the ticket contains all the information necessary to produce a FormsIdentity (this doesn’t hold true for roles, unless you specifically enable roles caching to a cookie). So you ought to look at the same approach. The first time you construct your siteUser object cache it within a signed cookie, then use the cookie to rehydrate your SiteUser properties on subsequent requests.

    If you do this then you can go one step further, replacing the Thread principle with your SiteUser, or at least a custom IPrincipal/IUser combination which has the same information as your SiteUser class would have.

    So inside AuthenticateRequest you’d have some flow like

    SiteUserSecurityToken sessionToken = null;
    if (TryReadSiteUserSecurityToken(ref sessionToken) && sessionToken != null)
    {
        // Call functions to attach my principal.
    }
    else
    {
        if (HttpContext.Current.User != null && 
            HttpContext.Current.User.Identity.IsAuthenticated && 
            HttpContext.Current.User.Identity is FormsIdentity)
        {
            // Get my SiteUser object
    
            // Create SiteUserSecurityToken
    
            // Call functions to attach my principal.
        }
    }
    

    And the function to attach the principal would contain something like

    HttpContext.Current.User = sessionSecurityToken.ClaimsPrincipal;
    Thread.CurrentPrincipal = sessionSecurityToken.ClaimsPrincipal;
    this.ContextSessionSecurityToken = sessionSecurityToken;
    

    You’ll want to make sure that the functions which write the Security Token to a cookie add, at a minimum, a checksum/MAC value, and, if you like, support encryption using the machine key if it’s configured to do so. The read functions should validate these values.

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

Sidebar

Related Questions

No related questions found

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.