I’m developing an intranet where I store data using the System.Web.Caching.Cache implementation of caching.
I can’t seem to get around the “Safe handle has been closed” error, that is produced with the following stacktrace:
[ObjectDisposedException: Safe handle has been closed]
System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success) +0
Microsoft.Win32.Win32Native.GetTokenInformation(SafeTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength) +0
System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeTokenHandle tokenHandle, TokenInformationClass tokenInformationClass) +203
System.Security.Principal.WindowsIdentity.get_User() +94
System.Security.Principal.WindowsIdentity.GetName() +127
System.Security.Principal.WindowsIdentity.get_Name() +42
The error is generated at the following line:
@if(intranet.Models.NyhederModels.isAdministrator(CachedVariables.getWP(User)))
Where getWP is as follows:
public static WindowsPrincipalEx getWP(System.Security.Principal.IPrincipal User)
{
Cache context = HttpRuntime.Cache;
WindowsPrincipalEx wp = context["windowsPrincipal_" + User.Identity.Name] as WindowsPrincipalEx;
if (wp == null)
{
wp = new intranet.Models.WindowsPrincipalEx(User.Identity);
context.Insert("windowsPrincipal_" + User.Identity.Name, wp, null, DateTime.Now.AddSeconds(15.0), Cache.NoSlidingExpiration);
}
return wp;
}
As you can see, the key into the cache takes the UserPrincipal, to make sure the cached data is on a per-user basis. I tried using HttpContext.Current, where the caching partially works, but since this happens on a per-request basis, the data is still retrieved once for every page.
The above code works flawlessly on the localhost but shows the “Safe handle has been closed” when run in production environment.
The data being cached is the result of a query into Windows Active Directory, if that makes any difference and User is an instance of System.Security.Principal.IPrincipal.
Any thoughts on this error, which only appeared after introducing the caching?
Thank you in advance
This is a little of an old question, but I ran across this today searching for “Safe handle has been closed principle caching” on google.
I am getting the same error when I added my WindowsPrincipal object to HttpContext.Cache so that I could reuse it for impersonation. I started getting this error. What I did to stop this error from happening is that instead of storing a WindowsPrincipal object I store just the WindowsIdentity.Token, and I create a new WindowsPrincipal with a new WindowsIdentiy, passing in the Cached WindowsIdentity.Token.