When ever I add the line below to my Startup class I get the exception below. This is a self hosted exe running from mono (Ubuntu). It works fine in windows. I've narrowed it down the UseCookieAuthentication call. I can instantiate the options without any issue. Any idea what is going on here? I have aspnet Identity working and EF6/MySql.
app.UseCookieAuthentication(new CookieAuthenticationOptions());
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Could not load type 'Microsoft.Owin.Security.DataProtection.DpapiDataProtector' from assembly 'Microsoft.Owin.Security, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. at Microsoft.Owin.Security.DataProtection.AppBuilderExtensions.CreateDataProtector (Owin.IAppBuilder,string[]) <0x00052> at Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware..ctor (Microsoft.Owin.OwinMiddleware,Owin.IAppBuilder,Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions) <0x00223> at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.Owin.OwinMiddleware,Owin.IAppBuilder,Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions) <0x00048> at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0x00067> at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <0x000d7>
--- End of inner exception stack trace --- at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in :0 at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in :0 at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in :0 at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in :0 at Microsoft.Owin.Builder.AppBuilder.BuildInternal (System.Type signature) [0x00000] in :0 at Microsoft.Owin.Builder.AppBuilder.Build (System.Type returnType) [0x00000] in :0 at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create (IAppBuilder builder) [0x00000] in :0 at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in :0 at Microsoft.Owin.Hosting.Engine.HostingEngine.Start (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in :0 at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.Starter.HostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.StartImplementation (IServiceProvider services, Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start[Startup] (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start[Startup] (System.String url) [0x00000] in :0 at HelloWorldNancy.Program.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Could not load type 'Microsoft.Owin.Security.DataProtection.DpapiDataProtector' from assembly 'Microsoft.Owin.Security, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. at Microsoft.Owin.Security.DataProtection.AppBuilderExtensions.CreateDataProtector (Owin.IAppBuilder,string[]) <0x00052> at Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware..ctor (Microsoft.Owin.OwinMiddleware,Owin.IAppBuilder,Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions) <0x00223> at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.Owin.OwinMiddleware,Owin.IAppBuilder,Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions) <0x00048> at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0x00067> at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <0x000d7>
--- End of inner exception stack trace --- at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in :0 at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in :0 at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000] in :0 at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in :0 at Microsoft.Owin.Builder.AppBuilder.BuildInternal (System.Type signature) [0x00000] in :0 at Microsoft.Owin.Builder.AppBuilder.Build (System.Type returnType) [0x00000] in :0 at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create (IAppBuilder builder) [0x00000] in :0 at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in :0 at Microsoft.Owin.Hosting.Engine.HostingEngine.Start (Microsoft.Owin.Hosting.Engine.StartContext context) [0x00000] in :0 at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.Starter.HostingStarter.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.StartImplementation (IServiceProvider services, Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start[Startup] (Microsoft.Owin.Hosting.StartOptions options) [0x00000] in :0 at Microsoft.Owin.Hosting.WebApp.Start[Startup] (System.String url) [0x00000] in :0 at HelloWorldNancy.Program.Main (System.String[] args) [0x00000] in :0
Okay this is happening because OWIN uses DpapiDataProtector by default and DPAPI is a windows API (Data Protection API) and does not work in mono. Luckily you can override the default in your cookie options. Below is an example where AesDataProtectorProvider is a custom IDataProtector that I found here: Using Oauth tickets across several services?
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
TicketDataFormat =
new SecureDataFormat<AuthenticationTicket>(DataSerializers.Ticket,
new AesDataProtectorProvider("testing"), TextEncodings.Base64)
});
With this code my project starts in Mono again.
UPDATE:
You can also have a custom IDataProtectionProvider and have all of owin use that with this:
app.SetDataProtectionProvider(new CustomIDataProtectionProvider());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With