Auth0 Home Blog Docs

Auth0.OidcClient.WinForms single threaded apartment



I’m trying to use the Auth0.OidcClient.WinForms to authenticate from within an Excel plugin. I’ve tried various things, but I keep getting an exception as follows:

"ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."

In the previous c# library (Auth0.WinformsWPF) I could pass a wrapper to the excel window to the LoginAsync function, but I can’t see how to achieve the same with the new library. Any ideas please?


Hey @jerrie1 any ideas about this one? :slight_smile:


If anyone else ever needs to access the auth0 libraries from an Excel plugin - it needs to be done from a CustomTaskPane.


I ran across this same issue today and I think I found a good solution. Here is what I found to be the problem:

When loading an add-in to Excel 2016 I found the System.Threading.SynchronizationContext.Current was null. This changes .NETs behavior when doing async/await. In the new version of the library Auth0.OidcClient.WinForms we are taking on a new depedency: IdentityModel and IdentityModel.OidcClient2. In that library we are doing a HttpClient.GetUrl(...).ConfigureAwait(false). This happens in two spots when kicking off auth:

It seems HttpClient is thread safe and any of its method calls will create a new thread which is run on an MTA thread. After the await occurs .NET checks for a SyncrhonizationContext, and finds it to be null so it falls back to TaskScheduler.Current. Here is a nice SO answer that I read:

In the end, what I did to fix this issue was add the following code on the button click (ribbon button) just before I initialized Auth0Client and called LoginAsync().

if (SynchronizationContext.Current == null)
  SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());