Frederik Crowdedlight Frederik Crowdedlight - 1 year ago 136
Linux Question

mod_mono asp MVC4 error: System.Security.SecurityException - Couldn't impersonate token

Hey There all you wonderful people.

I have developed an small webapp in MVC4, framwork 4,5. It all works like a charm when debugging with Visual Studio 2015. However I do not have an Windows server, but an Linux (ubuntu 14.04 VPS) and I wish to deploy there.

I have latest mono and mod_mono running with apache2. It seems to execute as it should, but I get some errors.

solved the first of them with adding

<customError mode="off">
too see the error and then adding a

But I am now stumped by this error:

Couldn't impersonate token.

Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): mscorlib.
Exception stack trace:
at System.Security.Principal.WindowsImpersonationContext..ctor (IntPtr token) <0x41ef9dd0 + 0x0006f> in <filename unknown>:0
at System.Security.Principal.WindowsIdentity.Impersonate (IntPtr userToken) <0x41ef9d50 + 0x0003c> in <filename unknown>:0
at System.CodeDom.Compiler.Executor.ExecWaitWithCapture (IntPtr userToken, System.String cmd, System.String currentDir, System.CodeDom.Compiler.TempFileCollection tempFiles, System.String& outputName, System.String& errorName) <0x41ef9c90 + 0x00038> in <filename unknown>:0
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.Compile (System.CodeDom.Compiler.CompilerParameters options, System.String compilerFullPath, System.String arguments, System.String& outputFile, System.Int32& nativeReturnValue) <0x41ef9a10 + 0x0010b> in <filename unknown>:0
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.FromFileBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] fileNames) <0x41ef5c40 + 0x005fb> in <filename unknown>:0
at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.CompileAssemblyFromFileBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] fileNames) <0x41ef5aa0 + 0x000c7> in <filename unknown>:0
at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromFile (System.CodeDom.Compiler.CompilerParameters options, System.String[] fileNames) <0x41ef58f0 + 0x0004d> in <filename unknown>:0
at System.Web.Compilation.AssemblyBuilder.BuildAssembly (System.Web.VirtualPath virtualPath, System.CodeDom.Compiler.CompilerParameters options) <0x41ee0a70 + 0x00991> in <filename unknown>:0
at System.Web.Compilation.AssemblyBuilder.BuildAssembly (System.Web.VirtualPath virtualPath) <0x41ee0a30 + 0x00023> in <filename unknown>:0
at System.Web.Compilation.BuildManager.GenerateAssembly (System.Web.Compilation.AssemblyBuilder abuilder, System.Web.Compilation.BuildProviderGroup group, System.Web.VirtualPath vp, Boolean debug) <0x41ed6b10 + 0x009b7> in <filename unknown>:0
at System.Web.Compilation.BuildManager.BuildInner (System.Web.VirtualPath vp, Boolean debug) <0x41eac440 + 0x00523> in <filename unknown>:0
at System.Web.Compilation.BuildManager.Build (System.Web.VirtualPath vp) <0x41eabbc0 + 0x0011b> in <filename unknown>:0
at System.Web.Compilation.BuildManager.GetCompiledType (System.Web.VirtualPath virtualPath) <0x41eaaa70 + 0x00093> in <filename unknown>:0
at System.Web.Compilation.BuildManager.GetCompiledType (System.String virtualPath) <0x41eaa690 + 0x00017> in <filename unknown>:0
at System.Web.HttpApplicationFactory.InitType (System.Web.HttpContext context) <0x41e9c4a0 + 0x0041b> in <filename unknown>:0

I haven't been able to fix that error. I have tried following steps:

  • published the project to a local folder and copied it to my linux

  • server taken the whole project and added to the linux server builded

  • whole project on linux with xbuild

Which all gives the same runtime error.

I have used Travis CI to check my code and it passes, with following configuration:

language: CSharp
solution: BannedFromHighsec.sln
- nuget restore BannedFromHighsec.sln
- xbuild /p:Configuration=Release /target:BannedFromHighsec BannedFromHighsec.sln /p:TargetFrameworkVersion="v4.5" /p:DebugSymbols=False

Pastebin for the Travis CI log:

So I am kinda thinking towards it's my code there's something wrong with. But I am not sure what it is as it's an simple app. Pretty much basic CRUD operations with SQlite and pulling data from an API and a second database. I'm not using logins or anything.

I hope someone can help me, I'm starting to get an handle on MVC, and would like to know how to actually get it deployed on linux, if possible :)

If you need any other information, configs, sourcecode let me know and I'll happily give it. Just not sure what is needed with that error code.

Answer Source

I'll start off by saying that my setup isn't identical to yours. I have an ASP.NET MVC website using .NET 4.5.2 on a Raspberry Pi 2, running Raspbian Jessie (Feb2016 edition). Stock Mono on this distro is 3.2.8 but I upgraded mine to 4.2.2.

So, I ran into the exact same error message as you did when I was trying to get a test site to run. What worked for me, was commenting out both of the <compiler> entries from my web.config like so below.

    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

Then after resetting the apache service

sudo service apache2 restart

And then after that, the MVC site loaded up just fine.

I can't say this is a perfect solution since I'm not sure why commenting out those lines of xml worked. If this works for you, that's great but I'm hoping someone can explain to me why those two lines of xml are the cause of this problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download