Publish web application with unsafe code
Asked Answered
V

3

7

I'm trying to publish a web application (with VS2012 Web) in which I need to run a vb script.

That script currently doesn't run correctly probably because of the lack of permissions. I am currently trying to run it as a user and supply some credentials. The password I have to provide must be in a System.Security.SecureString which needs a char* to be created.

When I run my app in debug everything works fine and as expected. But when comes the time to publish the app to the server, it says :

1>C:\SVN\...\Default.aspx.cs(108,21,108,27): error CS0227: Unsafe code may only appear if compiling with /unsafe

I have allowed for unsafe code in the project properties, but I don't know why VS sees that property when debugging and not when publishing.

When I hit publish, I see the CS0227 error in the error list but it stays for only 1 second, then it disappears... It seems like that second is enough to make the build fail.

Any ideas as to what the problem is?

Here is my code :

Process proc = new Process();
ProcessStartInfo psi = new ProcessStartInfo("C:\\Windows\\System32\\cscript.exe", "\"" + scriptPath + "\" " + args);
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;

psi.Domain = "MyDomain";
psi.UserName = "MyUsername";

System.Security.SecureString hashPwd;

unsafe
{
    // Instantiate a new secure string. 
    fixed (char* pChars = pwd)
    {
        hashPwd = new System.Security.SecureString(pChars, pwd.Length);
    }
}

psi.Password = hashPwd;


//Set the process' start info
proc.StartInfo = psi;

//We start the script.
proc.Start();
Vallie answered 15/5, 2013 at 14:2 Comment(0)
F
9

It's very simple to publish with a unsafe code

Here is how you do it: enter image description here

Fayth answered 29/10, 2013 at 16:4 Comment(2)
Thanks for the answer, but that flag is only to tell C# to compile the unsafe code... The solution that worked for me was just to remove the unsafe code and give up using pointers explicitly in C# :)Vallie
All Configurations and Allow Safe Code should be selected.Revolving
A
6

It seems when you publish it ignores the project setting of unsafe, so you need to manually open your .CSPROJ file and include:

  <PropertyGroup>
    ...
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

That solved it for me.

Googled this issue but could not find anything. Checked the *.targets files for "unsafe" which led me to the "AllowUnsafeBlocks" tag.

Antonantone answered 26/9, 2015 at 23:28 Comment(0)
Y
2

Please add this code in Web.config file and check it is working for you

<compilers>
<compiler language="c#;cs;csharp" extension=".cs" compilerOptions="/unsafe" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</compilers>
Yoo answered 15/5, 2013 at 14:26 Comment(2)
No, same problem... I had to enclose it in <system.codedom> tags for it to compile, and it still wont publish... The weird thing is a get a compiler error (CS0227) for about 1 second and then it disappears.Vallie
me too have the same problem. Is there any solution to prevent it ?Bundle

© 2022 - 2024 — McMap. All rights reserved.