Re: InvalidCastException Using AzMan from ASP.NET

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Chris Bilson (cbilson_at_crimes.net)
Date: 02/05/04


Date: 5 Feb 2004 11:08:18 -0800

Hi Ying-Shen,

   Thanks for your reply.

> From my understanding, you initialized the IAzApplicationStore at startup,
> then call the method on IAzApplication later when the AuthenticateRequest
> event fired.

Correct.
 
> You may read the follow article first and try if the ASP.NET sample code
> works properly on your system.

Which article is that?

> If the sample works fine, I suspect the
> problem might still caused by thread mismatch, you may try taking a look at
> the IIS log or dig deep into the InvalidCastException, to get the error
> code(I mean HRESULT returned by Com runtime).

0x80004002. There is no inner or base exception.

In addition to the System.Threading.Thread.GetHashCode() checking I
mentioned, I added calls to Kernel32's GetCurrentThreadId(), for
diagnostics, and verified that when the error occurs, I am on the same
Windows thread, as well as the same .NET thread.

> In addition, I'm not clear what your "static variable" mean, you may try
> storing the IAzApplication in Session property instead.

I tried that, but had the same problem. What I meant by static
variable was something like:

public class SecurityMgr
{
   ...
   
   // NOTE: Putting this in App State or Session State did not help
   private static IAzApplication azApp_ = null;

   // Called from my Global.asax's Application_Start
   public void Init(HttpContext)
   {
      ...
      AzAuthorizationStore store = new AzAuthorizationStoreClass();
      store.Initialize(0, @"msxml://" + storeFilename, null);
      azApp_ = store.OpenApplication(appName, null);
      ...

   }

   // Called from my Global.asax's Application_AuthenticateRequest
   public void InitContext(HttpContext ctxt)
   {
     ...
     IPrincipal p = HttpContext.Current.User;
     WindowsIdentity winID = p.Identity as WindowsIdentity;

     // NOTE: This is where the error occurs
     IAzClientContext azCtxt =
azApp_.InitializeClientContextFromToken((uint) winID.Token, null);

     CallContext.SetData("SecuityMgr:ClientContext", azCtxt);
   }

   // Called from anywhere
   public bool AccessCheck(string operationName)
   {
      bool rv = false;

      int opID = GetOperationIDSomehow(operationName);
      IAzClientContext ctxt =
CallContext.GetData("SecurityMgr:ClientContext")
        as IAzClientContext;
      object[] res = ctxt.AccessCheck("something contextual", null,
         new object[]{operationID}, null, null, null, null, null);
      if (res.Length > 0)
         rv = 0 == (int) res[0];

      if (rv) {
        // Do some extra checking based on some application business
rules
      }
      
      return rv;
   }
}