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.S开发者_StackOverflow社区ecurity.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.
The above answer didn't work for me. Actually, it worked initially, but the site needs to reset the user ID multiple times behind the scenes as the site launches a page. On the 3rd time the code is called to create a Principle with the Token, I get this error "Invalid token for impersonation - it cannot be duplicated." No clue.
HttpContext.Current.User = (new WindowsPrincipal(new WindowsIdentity((IntPtr)Session["IdentityToken"])));
[edited 11/21/2012] I finally had to use some brute force to solve my problem. I simply save only the User ID (a string) in session and recreate the Identity/Principal each time. I'm suspicious of an automatic MS Windows update (on 10/18) that might have caused the issue all of a sudden, but not sure at this time.
WindowsIdentity identity = new WindowsIdentity(HttpContext.Current.Session["UserID"].ToString());
HttpContext.Current.User = new WindowsPrincipal(identity);
Following on from @Prethen's answer (which helped me out!), here's the code that was causing the “Safe handle has been closed” exception for me:
return RequestContext.Principal.Identity.Name;
Here's what fixed it:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
HttpContext.Current.User = new WindowsPrincipal(identity);
return RequestContext.Principal.Identity.Name;
精彩评论