Mono 3.4.1中的UserCookieAuthentication [英] UserCookieAuthentication in Mono 3.4.1
问题描述
每当我将以下行添加到Startup类中时,都会出现以下异常.这是一个从mono(Ubuntu)运行的自托管exe.在Windows中工作正常.我将其范围缩小到UseCookieAuthentication调用.我可以实例化这些选项,而不会出现任何问题.知道这里发生了什么吗?我可以使用aspnet Identity和EF6/MySql.
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());
未处理的异常:
System.Reflection.TargetInvocationException:调用的目标引发了异常. ---> System.TypeLoadException:无法从程序集"Microsoft.Owin.Security,版本= 2.1.0.0,文化=中性,PublicKeyToken = 31bf3856ad364e35"中加载类型"Microsoft.Owin.Security.DataProtection.DpapiDataProtector".
在Microsoft.Owin.Security.DataProtection.AppBuilderExtensions.CreateDataProtector(Owin.IAppBuilder,string [])<0x00052>
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>
-内部异常堆栈跟踪的结尾-
在System.Reflection.MonoMethod.Invoke处(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder绑定程序,System.Object []参数,System.Globalization.CultureInfo文化)[0x00000]在:0中
在System.Reflection.MethodBase.Invoke处(System.Object obj,System.Object []参数)[0x00000]在:0中
在System.Delegate.DynamicInvokeImpl(System.Object [] args)[0x00000]中的位置:: 0
在System.MulticastDelegate.DynamicInvokeImpl(System.Object [] args)[0x00000]中的位置:: 0
在0的System.Delegate.DynamicInvoke(System.Object [] args)[0x00000]处
在Microsoft.Owin.Builder.AppBuilder.BuildInternal(System.Type签名)[0x00000]中的位置:: 0
在Microsoft.Owin.Builder.AppBuilder.Build(System.Type returnType)[0x00000]中的位置:: 0
在Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder构建器)[0x00000]中的:0中
在Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(Microsoft.Owin.Hosting.Engine.StartContext上下文)中:[0x00000]在:0中
在Microsoft.Owin.Hosting.Engine.HostingEngine.Start(Microsoft.Owin.Hosting.Engine.StartContext上下文)中:[0x00000]在:0中
在Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000]在:0中
在Microsoft.Owin.Hosting.Starter.HostingStarter.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0
在Microsoft.Owin.Hosting.WebApp.StartImplementation中(IServiceProvider服务,Microsoft.Owin.Hosting.StartOptions选项)在[0x00000]中:: 0
在Microsoft.Owin.Hosting.WebApp.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0
在Microsoft.Owin.Hosting.WebApp.Start [启动](Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0
在Microsoft.Owin.Hosting.WebApp.Start [Startup](System.String url)中的[0x00000] :: 0
在HelloWorldNancy.Program.Main(System.String [] args)中的[0x00000]在:0中
[错误]致命异常:System.Reflection.TargetInvocationException:调用的目标引发了异常. ---> System.TypeLoadException:无法从程序集"Microsoft.Owin.Security,版本= 2.1.0.0,文化=中性,PublicKeyToken = 31bf3856ad364e35"中加载类型"Microsoft.Owin.Security.DataProtection.DpapiDataProtector".
在Microsoft.Owin.Security.DataProtection.AppBuilderExtensions.CreateDataProtector(Owin.IAppBuilder,string [])<0x00052>
--- 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>
-内部异常堆栈跟踪的结尾- 在System.Reflection.MonoMethod.Invoke处(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder绑定程序,System.Object []参数,System.Globalization.CultureInfo文化)[0x00000]在:0中 在System.Reflection.MethodBase.Invoke处(System.Object obj,System.Object []参数)[0x00000]在:0中 在System.Delegate.DynamicInvokeImpl(System.Object [] args)[0x00000]中的位置:: 0 在System.MulticastDelegate.DynamicInvokeImpl(System.Object [] args)[0x00000]中的位置:: 0 在System.Delegate.DynamicInvoke(System.Object [] args)[0x00000]中的位置:: 0 在Microsoft.Owin.Builder.AppBuilder.BuildInternal(System.Type签名)[0x00000]中的位置:: 0 在Microsoft.Owin.Builder.AppBuilder.Build(System.Type returnType)[0x00000]中的位置:: 0 在Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder构建器)[0x00000]中的:0中 在Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(Microsoft.Owin.Hosting.Engine.StartContext上下文)中:[0x00000]在:0中 在Microsoft.Owin.Hosting.Engine.HostingEngine.Start(Microsoft.Owin.Hosting.Engine.StartContext上下文)中:[0x00000]在:0中 在Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0 在Microsoft.Owin.Hosting.Starter.HostingStarter.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0 在Microsoft.Owin.Hosting.WebApp.StartImplementation中(IServiceProvider服务,Microsoft.Owin.Hosting.StartOptions选项)在[0x00000]中:: 0 在Microsoft.Owin.Hosting.WebApp.Start(Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000] :: 0 在Microsoft.Owin.Hosting.WebApp.Start [启动](Microsoft.Owin.Hosting.StartOptions选项)中的[0x00000]:0 在Microsoft.Owin.Hosting.WebApp.Start [Startup](System.String url)中的[0x00000] :: 0 在HelloWorldNancy.Program.Main(System.String [] args)[0x00000]中,位于:0
--- 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
推荐答案
发生这种情况的原因是,OWIN默认使用DpapiDataProtector,而DPAPI是Windows API(数据保护API),因此不能单声道运行.幸运的是,您可以在Cookie选项中覆盖默认值.下面是一个示例,其中AesDataProtectorProvider是我在此处找到的自定义IDataProtector:在多个应用程序中使用Oauth票证服务?
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)
});
使用此代码,我的项目再次从Mono开始.
With this code my project starts in Mono again.
更新:
您还可以拥有一个自定义IDataProtectionProvider,并让所有owin都使用它:
You can also have a custom IDataProtectionProvider and have all of owin use that with this:
app.SetDataProtectionProvider(new CustomIDataProtectionProvider());
这篇关于Mono 3.4.1中的UserCookieAuthentication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!