Scott Hanselman

System.Threading.Thread.CurrentPrincipal vs. System.Web.HttpContext.Current.User or why FormsAuthentication can be subtle

September 09, 2004 Comment on this post [11] Posted in ASP.NET | Bugs
Sponsored By

Warning: I find this fascinating and amazing as a caused a suble bug and was generally bizarre today.  You likely don't care. :)

I have some code in an ASP.NET custom FormsAuthentication Login that looks something like this:

// This principal will flow throughout the request.
VoyagerPrincipal principal = new VoyagerPrincipal(yada, yada, yada);

// Attach the new principal object to the current HttpContext object
HttpContext.Current.User = principal;

It it called on the Global.asax's AuthenticateRequest so everything is all setup before the Page's events fire.  It provides a custom IPrincipal that integrates our eFinance Server with ASP.NET.  It's quite a lovely subsystem, IMHO.

Other operations count on being able to get this 'Call Context' IPrincipal from the current thread at any time.  In another section of code someone was doing this in the MIDDLE of the HttpRequest (somewhere in the Page_Load) after having JUST called the routine above for the first time:

return Thread.CurrentPrincipal as VoyagerPrincipal;

Assuming, of course that the Thread's CurrentPrincipal is that same Principal.  And 99.999% percent of the time it is, except when it isn't at all.

In the instance where someone calls the first chunk of code then expects to be able to call the second chunk within the same HttpRequest, the Thread.CurrentPrincipal contains a GenericPrincipal populated much earlier by the HttpApplication.  (Or a WindowsPrincipal, depending on your settings).

  • When the first chunk of code runs in the Global.asax's AuthenticateRequest these two properties ARE in fact the same object
  • When the first chunk of code runs in the context of a Page (read: later!) these properties are NOT the same object.

Why? Reflector tells us in the HttpApplication's internal OnThreadEnter:

internal void OnThreadEnter()
{
      this._savedContext = HttpContextWrapper.SwitchContext(this._context);
      this._context.Impersonation.Start(false, true);
      HttpRuntime.RequestTimeoutManager.Add(this._context);
      this.SetPrincipalOnThread(this._context.User);
      this.SetCulture(false);
}

internal void SetPrincipalOnThread(IPrincipal principal)
{
      if (!this._restorePrincipal)
      {
            this._restorePrincipal = true;
            this._savedPrincipal = Thread.CurrentPrincipal;
      }
      Thread.CurrentPrincipal = principal;
}

I had assumed, wrongly, that these two objects were coming from the same object reference always.  In fact, they are early on, but you can (as I did) change one without changing the other.  So, the first chunk of code becomes this:

// This principal will flow throughout the request.
VoyagerPrincipal principal = new VoyagerPrincipal(yada, yada, yada);

// Attach the new principal object to the current HttpContext object
HttpContext.Current.User = principal;

// Make sure the Principal's are in sync
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;

And all is right with my world, and the folks can continue to get the expected behavior when doing a "mid-page" FormAuthentication login.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Betsy says I'm dapper, so it must be so!

September 07, 2004 Comment on this post [2] Posted in ASP.NET
Sponsored By

Rock on.  Betsy Aoki, Manager of the MSDN Community Site, says I'm dapper. Phil Weber had lunch with her on campus recently and she blogged about the experience of having a "geek fan." 

Phil turned out to be quite normal (if anyone in this business can be considered normal, that goes double for consultants) and I realized he works with Scott Hanselman, a guy I met at the Portland Nerd Dinner a bunch of us road-tripped to. Scott stuck out in my mind because he was so freakin' dapper -  the only guy in the food court with a full business suit on. [Betsy]

You just made my day, Betsy.  I'll buy you lunch in the MSFT Food Court next time I'm in Redmond. I may even read you some of my poetry as I hear you read a mean poem yourself.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Paint.NET joins the ranks of "tools that Microsoft won't improve, so someone else did it themselves"

September 07, 2004 Comment on this post [9] Posted in Musings
Sponsored By

Fabulous.  Not only can I replace the large-unchanged-since-1994 Notepad.exe with Notepad2, but Mike finds Paint.NET for me, and with a 5 minute evaluation I've replaced my Paint.exe with Paint.NET.  It's as simple and as elegant as Paint Shop Pro (the early version, before they threw in the Kitchen Sink and it got all sassy and quasi-vectory) used to me.

+1 for Paint.NET!

While I'm replacing all my default Windows Accessories, what are some other freeware/.NET replacements?

  • Calculator?
  • Hyperterminal?
  • Backup?
  • Wordpad?
  • Sound Recorder?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Minimed Pump Equipment

September 06, 2004 Comment on this post [2] Posted in Diabetes
Sponsored By

If you (or you know someone who) needs Minimed Insulin Pump Equipment for the 507/8 Pump or compatible, I have a pile of Reservoirs, Silhouette Infusion Sets and Quickset Infusion sets that are not being used as I just got a Paradigm Insulin Pump. I'll part with them for the cost of my co-pay. 

Email me if you're interested. Even better if you're in Portland, I'll bring them to you.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

I'm in love, and her name is "SlickRun" - The CommandLine in a Windows World

September 04, 2004 Comment on this post [9] Posted in ASP.NET | Tools
Sponsored By

A while back (I mentioned this to a few folks, so there's prior art) I started working on the "ZenBar."  The basic idea was that I spend a HELL of a lot of time on in the Run Dialog (Window-R) or at the command line using CD and Tab Auto-Completion.  It's just faster for me to use a hotkey or command line to move around for many things than use the mouse.  ZenBar was to be simple. A floating, partially transparent text box that would index my world and give me autocompletion for all things. It was a little like ActiveWords and a little like Dave's Search Bar (both are great, by the way, but I just wasn't willing to LIVE in them.

But, as projects go, I got about 40% done then stopped.  Too many other things. 

Yesterday I checked out Mike G's Daily Grind via Tim Marman and HOLY CRAP.  Someone wrote it. They wrote it better than I could have, and they did it in Delphi. 

It's SlickRun, and it rocks my world. Here's why, and what I did to fit it into my life.

  • It's a resizable, floating text box. I changed the font to Lucida Console, Bold 14 point.  When you're running 1600x1200 you like a larger font.
  • It does autocomplete for commands and for directories. You can type C:\doc and you're in documents and settings. Awesome.
  • You can setup MagicWords like "Mail" for Outlook, or Google to search, etc.
  • This is the BEST: You an have Multi-MagicWords like "morning' that will launch six web pages, your Mail, and Microsoft Money.  The "Morning" Macro.  Fabulous.
  • It's an inline calculator.
  • It can be bound to a Hotkey - I've got it bound to Window-Q, and it can Grab Window-R if you like.
  • It includes Jot notes for quick notes, easier than notepad, less hassle than a yellow sticky.

What a wonderful thing.  Check out SlickRun. It's fast and small.  Faster than Dave's Search, and easier than ActiveWords. I shall delete my half-assed attempt at the ZenBar immediately and bow to Bayden Systems.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.