Web API - Web Forms Project Security Error - asp.net

I am having a problem with a project with Web API.
Inside of the global.asax file I have this and it is giving an error that says
An exception of type 'System.TypeLoadException' occurred in System.Web.Http.WebHost.dll but was not handled in user code
Additional information: Inheritance security rules violated while overriding member: 'System.Web.Http.WebHost.Routing.HostedHttpRoute.get_RouteTemplate()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
On this line
RouteTable.Routes.MapHttpRoute(name:="DefaultApi", routeTemplate:="api/{controller}/{id}", defaults:=New With { _
Key .id = RouteParameter.[Optional] _
})
I upgraded some packages. This used to be my packages.config file
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net45" />
They are now upgraded to this:
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.30506.0" targetFramework="net45" />
Any idea why I just started getting this error after upgrading?
Thank you very much!

I have project A that references project B.
In project B there are controllers and there I am calling RouteTable.Routes.MapHttpRoute for these controllers.
I have found in my case that problem was that in project A WebApi NuGet package version was higher than in referenced project B.

Related

How to determine valid NuGet package version combinations for NanoFramework

I am building a .NET NanoFramework demo using the ESP32_VROOM_32 board. I'm having trouble figuring our the correct combination of NuGet packages to avoid getting an assembly mismatch error when I try to debug my solution on the actual hardware. This error I keep getting when I push to the device is:
[Found assemblies mismatches when checking for deployment pre-check.]
The combination of packages I currently have installed are:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AMQPNetLite.nanoFramework" version="2.3.0.39" targetFramework="netnanoframework10" />
<package id="nanoFramework.CoreLibrary" version="1.7.3-preview.4" targetFramework="netnanoframework10" />
<package id="nanoFramework.Hardware.Esp32" version="1.2.1-preview.12" targetFramework="netnanoframework10" />
<package id="nanoFramework.ResourceManager" version="1.0.0-preview.15" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.4.2-preview.8" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Native" version="1.4.0-preview.1" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Collections" version="1.0.1-preview.7" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Math" version="1.2.0-preview.16" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Net" version="1.4.1-preview.21" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Text" version="1.0.0-preview.22" targetFramework="netnanoframework10" />
</packages>
My question is, is there a reliable systematic way to determine which combination of NuGet packages will work together with the firmware installed on the device?
I have tried many different combinations and can't get anything to work.
Currently, I have 1.4.0-preview.208 firmware installed on the ESP32, which appears to be the latest stable version.
There is no direct relation between the versioning of the C# libraries and the native firmware implementation. That's because each part has it's own separate development cycle.
The declaration is on which native version it requires. From managed to native, not the other way around.
This is showned on the NuGet description. If you want to learn more about this, take a look at this blog post here.
In the meantime know that stable releases have been published for all class libraries and firmware. The links for the firmware images have been updated.
Each NuGet package has a declaration on which other packages it depends on and which version. There is no other way to determine a valid package combination.
The fw version you have loaded it's not a stable version, therefore the "preview" in the version. But neither are the NuGets. Which is all OK: if you are using the "preview" firmware you have to use the "preview" NuGets.
Can you post here the error message about the version mismatch that you get? This will help determine what is wrong there.

Extension method IsNullOrWhiteSpace with the same namespace is placed in two assemblies

I have ASP.Net MVC project. The project contains WebGrease, packages.config:
<package id="WebGrease" version="1.5.2" targetFramework="net462" />
Recently because of (https://www.nuget.org/packages/BundleTransformer.MicrosoftAjax/) I added AjaxMin into my packages.config:
<package id="AjaxMin" version="5.14.5506.26202" targetFramework="net461" />
In my code, I am using IsNullOrWhiteSpace() extension method. The method actually exists in these two libraries:
Visual Studio cannot decided which one has to be used.
I cannot remove the reference to WebGrease, because if I do so, I have the following exception:
How can I solve the conflict?
Thank you
You can use string.IsNullOrWhitespace() directly.

Trying to bind configuration: System.ComponentModel.TypeConverter can't be loaded

I am trying to use ASP.NET Core configuration (specifically Microsoft.Extensions.Configuration.Json) in a console application. When I call IConfigurationRoot.Bind() it throws a FileNotFound exception for the assembly System.ComponentModel.TypeConverter.
To my knowledge TypeConverter is in the System.ComponentModel namespaces in the System assembly, not the fictitious System.ComponentModel.TypeConverter assembly.
This appears to be a bug. Or am I missing something?
I'm targeting .NET 4.5.2, have referenced System, and have the following NuGet packages installed:
<package id="Microsoft.Extensions.Configuration" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Configuration.Binder" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Configuration.FileExtensions" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Configuration.Json" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Extensions.Primitives" version="1.0.0" targetFramework="net452" />
Is there a workaround or additional steps?
This also does not work:
ConfigurationBinder.Bind(config, myconfig);
It does not seem to matter whether my JSON represents string properties or a struct such as a TimeSpan.
I've been used to TypeConverter being in System. I poked around NuGet and found there is indeed an assembly, System.ComponentModel.TypeConverter as part of .NET Core. Installing that package fixed my problem.

OWinStartup, OWinStartupAttribute not found & 'Owin.IAppBuilder' does not contain a definition for 'Map'

After upgrading my asp.net webform website from Owin 2.1.0 to Owin 3.0.1, I have the fore-mentionned compilation errors... I'm trying to use also SignalR 2 and CORS. Here's my packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jQuery" version="2.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR" version="2.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
My OwinStartup class is defined in web.config in appsettings as follow:
<add key="owin:AppStartup" value="SignalRStartup, App_Code" />
And this is what the class look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;
using Microsoft.Owin;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using System.IO;
[assembly: OwinStartup(typeof(SignalRStartup))]
public class SignalRStartup
{
public void Configuration(IAppBuilder app)
{
// Enable detailed errors (remember to remove it
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
//hubConfiguration.EnableCrossDomain = true;
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR(hubConfiguration);
}
}
Also note that I cannot use app.Map("/signalr", map => { ... });...
EDIT:
I have found in my web.config those lines, that I didn't have before upgrading:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I'm about to give up, please if you have any idea...
Yay ! Found it ! Many Great Thanks to this post:
The type or namespace IAppBuilder could not be found(missing using a directive pr an assembly reference).
My problem was resolved by reinstalling Microsoft.owin like Иво Недев suggested:
PM> Update-Package Microsoft.Owin -Reinstall
Removing 'Microsoft.Owin 3.0.1' from ww.
Value does not fall within the expected range.
Successfully removed 'Microsoft.Owin 3.0.1' from ww.
Removing 'Owin 1.0' from ww.
Successfully removed 'Owin 1.0' from ww.
Uninstalling 'Microsoft.Owin 3.0.1'.
Successfully uninstalled 'Microsoft.Owin 3.0.1'.
Uninstalling 'Owin 1.0'.
Successfully uninstalled 'Owin 1.0'.
Attempting to resolve dependency 'Owin (≥ 1.0)'.
Installing 'Owin 1.0'.
Successfully installed 'Owin 1.0'.
Installing 'Microsoft.Owin 3.0.1'.
You are downloading Microsoft.Owin from Microsoft, the license agreement to which is available at http://www.microsoft.com/web/webpi/eula/net_library_eula_enu.htm. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.Owin 3.0.1'.
Adding 'Owin 1.0' to ww.
Successfully added 'Owin 1.0' to ww.
Adding 'Microsoft.Owin 3.0.1' to ww.
Successfully added 'Microsoft.Owin 3.0.1' to ww.
My head can stop bleeding now :-P

System.Net.Http could not be resolved in the currently targeted framework .NET 4.0

Got a nasty error after moving a solution from Visual Studio 2010 to 2012. Build is fine and everything works perfect localhost. I have one new MVC4 project that requires .NET 4.5 so I installed the 4.5 framework on the build server. The build is good but MSBuild is failing to copy DotNetOpenAuth.Core.dll from its package location to the Bin dir. The issue is because, though the particular project targets the 4.0 framework, I believe MSBuild is checking the dependency against .NET 4.5 framework System.Net.Http assembly as part of its CoreBuild.
warning code="MSB3268" The primary reference \packages\DotNetOpenAuth.Core.4.3.0.13117\lib\net40-full\DotNetOpenAuth.Core.dll" could not be resolved because it has an indirect dependency on the framework assembly "System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "c:\cc\kazork\code\trunk\src\packages\DotNetOpenAuth.Core.4.3.0.13117\lib\net40-full\DotNetOpenAuth.Core.dll" or retarget your application to a framework version which contains "System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Here is my package config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ChardinJs" version="1.0.3" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
<package id="DotNetOpenAuth.AspNet" version="4.3.0.13117" targetFramework="net40" />
<package id="DotNetOpenAuth.Core" version="4.3.0.13117" targetFramework="net40" />
<package id="DotNetOpenAuth.OAuth.Consumer" version="4.3.0.13117" targetFramework="net40" />
<package id="DotNetOpenAuth.OAuth.Core" version="4.3.0.13117" targetFramework="net40" />
<package id="DotNetOpenAuth.OpenId.Core" version="4.3.0.13117" targetFramework="net40" />
<package id="DotNetOpenAuth.OpenId.RelyingParty" version="4.3.0.13117" targetFramework="net40" />
<package id="EntityFramework" version="5.0.0" targetFramework="net40" />
<package id="flexigrid" version="1.1.0" targetFramework="net40" />
<package id="jQuery" version="1.9.1" targetFramework="net40" />
<package id="Microsoft.AspNet.FriendlyUrls.Core" version="1.0.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Membership.OpenAuth" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Core" version="1.1.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.JS" version="1.1.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Owin" version="1.1.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="1.1.1" targetFramework="net40" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" />
<package id="Microsoft.Owin.Host.SystemWeb" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.5" targetFramework="net40" />
<package id="Owin" version="1.0" targetFramework="net40" />
<package id="PayPalCoreSDK" version="1.1.1" targetFramework="net40" />
<package id="PayPalMerchantSDK" version="2.3.98" targetFramework="net40" />
<package id="Select2.js" version="3.3.2" targetFramework="net40" />
<package id="toastr" version="1.2.2" targetFramework="net40" />
<package id="Twitter.Bootstrap" version="2.3.2" targetFramework="net40" />
<package id="WebGrease" version="1.3.0" targetFramework="net40" />
</packages>
I have System.Net.Http referenced and it is copied to the bin. The project in question is targeting the 4.0 framework. My version System.Net.Http is for the 4.0 framework.
So even though I was copying System.Net.Http .NET 4.0 via the Nuget package that is added when installed the DotNetOpenAuth packages, MsBuild on my build server was checking the DotNetOpenAuth.Core dependency and checking against .NET assemblies installed on the machine not what was being copied to the Bin. In my case:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
And there was no System.Net.Http assembly in my
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
directory so I believe MsBuild attempted to look in the newly installed .NET 4.5 assemblies, on my machine..\Assemblies\Microsoft\Framework.NETFramework\v4.5 where there is a System.Net.Http.dll and that is why, instead of getting an outright error that System.Net.Http.dll for .NET 4.0 targets does not exist, I got a warning about DotNetOpenAuth.Core.dll's dependency on System.Net.Http, which can be found but is not for the 4.0 framework as the project was targeting.
I simply copied System.Net.Http .NET 4.0 DLL from the package to
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\.NETFramework\v4.0
on my build server and MsBuild was able to compare to the correct version System.Net.Htpp for 4.0 .NET framework dependency check.
If this fails, you may also need to apply the .Net v4.5.1 dev pack as this supports multiple targeting. I had 4.5.1 installed and the assemblies copied and still had this error. Installing the dev pack from http://www.microsoft.com/en-us/download/details.aspx?id=40772 fixed this issue and now builds under server 2012 r2.

Resources