Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created November 25, 2015 10:49
Show Gist options
  • Save lbargaoanu/1f2547cbbbfa9ee6aa43 to your computer and use it in GitHub Desktop.
Save lbargaoanu/1f2547cbbbfa9ee6aa43 to your computer and use it in GitHub Desktop.
ASP.NET Crash. Invalid token for impersonation - it cannot be duplicated.
using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Security.Principal;
using System.Threading;
using System.Web.Mvc;
public class HomeController : Controller
{
static Timer timer = new Timer(_ =>
{
try
{
// https://social.msdn.microsoft.com/Forums/en-US/55c24ac5-b051-4559-bb3d-9cfba8f8c9ef/updated-invalid-token-for-impersonation-it-cannot-be-duplicated
// http://stackoverflow.com/questions/8993293/windowsidentity-impersonate-in-asp-net-randomly-invalid-token-for-impersonation
// https://support.microsoft.com/en-us/kb/982262
// run this in IIS with Windows authentication (not sure whether it's required)
// this crashes eventually when serializing the windows identity from the LogicalCallContext.Principal
// that identity is disposed by System.Web.RootedObjects.ReleasePrincipal
// setting the principal to null avoids the problem
//Thread.CurrentPrincipal = null;
// do remoting work
SqlDependency.Start("connection string");
Trace.WriteLine("OK " + Thread.CurrentPrincipal.Identity.IsAuthenticated);
}
catch(Exception ex)
{
Trace.WriteLine(ex);
}
}, null, 0, 1000);
public ActionResult Index()
{
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment