Failed to configure "IApplicationBuilder UseOwin"

As stated in the UseOwin paper , I'm trying to implement UseOwin in Startup.cs. I am trying to use / port IAppBuilder (Microsoft.Owin.Builder.AppBuilder) inside IApplicationBuilder (Microsoft.AspNetCore.Builder.IApplicationBuilder) . I had legacy code using IAppBuilder that works fine on .Net Framework 4.5.

I saw a couple of examples about using IAppBuilder in IAplicationBuilder, for example. example 1 example 2 . These attempts concerned .netcore 1.1, not .net core 2.0. Maybe this is the reason why I can not port.

Share your thoughts on whether I am trying to achieve something that is not currently possible in .net core 2.0, or there is some error in my code.

Note: I am using dotnetcore 2.0 with Visual Studio 2017

Error

I get the following error.

return owinAppBuilder.Build, Task β†’ (); TypeLoadException: Failed to load type 'System.Security.Cryptography.DpapiDataProtector' from the assembly 'System.Security, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a ".

My attempt

  app.UseOwin(setup => setup(next => { var owinAppBuilder = new AppBuilder(); var aspNetCoreLifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime)); new AppProperties(owinAppBuilder.Properties) { OnAppDisposing = aspNetCoreLifetime?.ApplicationStopping ?? CancellationToken.None, DefaultApp = next, AppName = "test" }; // Only required if CORS is used, configure it as you wish var corsPolicy = new System.Web.Cors.CorsPolicy { AllowAnyHeader = true, AllowAnyMethod = true, AllowAnyOrigin = true, SupportsCredentials = true }; //corsPolicy.GetType() // .GetProperty(nameof(corsPolicy.ExposedHeaders)) // .SetValue(corsPolicy, tusdotnet.Helpers.CorsHelper.GetExposedHeaders()); owinAppBuilder.UseCors(new Microsoft.Owin.Cors.CorsOptions { PolicyProvider = new CorsPolicyProvider { PolicyResolver = context => Task.FromResult(corsPolicy) } }); PublicClientId = "self"; OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new Microsoft.Owin.PathString("/Login"), Provider = new MyServiceProvider(PublicClientId), AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60), AllowInsecureHttp = true, RefreshTokenProvider = new MyRefreshTokenProvider(), }; owinAppBuilder.UseOAuthBearerTokens(OAuthOptions); //owinAppBuilder.UseTus(context => new DefaultTusConfiguration //{ // // Excluded for brevity, use the same configuration as you would normally do //}); return owinAppBuilder.Build<Func<IDictionary<string, object>, Task>>(); })); 
+2
source share
2 answers

Microsoft.Owin and related packages have no goals for .NET Core, no for .NET Standard. All they have is dlls aimed at full .NET. You can reference such libraries from your .NET Core project, but they are not guaranteed to work, as you can see, because the API (a set of classes \ methods \ signatures) is different for all .NET and .NET Core. Visual Studio will even show a warning when you do this, for example:

The package 'Microsoft.Owin 3.1.0' was restored using '.NETFramework, Version = v4.6.1' instead of the project target scheme '.NETCoreApp, Version = v2.0. This package may not be compatible with your project.

There is a Microsoft.AspNetCore.Owin package, and you can use the OWIN middleware in the .NET Core application, as described in the first link, but almost all that it provides is the UseOwin extension UseOwin . There is no type of AppBuilder , etc., and there are no Microsoft.AspNetCore.Owin.Cors packages or the like. Thus, you must either implement all this yourself (there is no reason, because you can use the same functions as in the framework of the main asp.net infrastructure), or wait for OWIN packages aimed at .NET Standard \ Core and do it (did not check, perhaps they already exist).

Thus, your code uses packages that are really not compatible with your target structure, as an exception that you show at runtime. So another answer (for some reason, top-down) is technically correct.

If you still want to use these packages reliably, you need to configure the full .NET Framework, not the .NET Core. To do this, open the .csproj file and change

 <TargetFramework>netcoreapp2.0</TargetFramework> 

For some versions of the .NET Framework that support the .NET Standard 2.0, for example:

 <TargetFramework>net47</TargetFramework> 

Then go to the nuget package manager, and if you have the microsoft.aspnetcore.all package (or other packages targeting .NET Core) - uninstall it, you won’t need it anyway. Then install the Microsoft.AspNetCore package and all other asp.net core packages you need (if they are not already installed). Rebuild, run, and everything will be fine.

This works because all (most?) AspNetCore packages target the .NET Standard, not the .NET Core, and you can use them in projects that target the full .NET Framework.

Please note that in doing this, you have an asp.net Core project, but not on .NET Core, with all the consequences that follow from this (it cannot work with dotnet run , on linux you need to work with mono, etc.) ,.

+1
source

Microsoft.Owin components will not work in dotnet core 2.0, they only work with .NET 4.5 +

-1
source

Source: https://habr.com/ru/post/1271123/


All Articles