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

  • Home
  • SEARCH
  • 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 6825071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:58:43+00:00 2026-05-26T21:58:43+00:00

I’m developing an intranet where I store data using the System.Web.Caching.Cache implementation of caching.

  • 0

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

  • 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-26T21:58:44+00:00Added an answer on May 26, 2026 at 9:58 pm

    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.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

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.