Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish web application with unsafe code

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();
like image 464
Bun Avatar asked May 15 '13 14:05

Bun


People also ask

How do I allow unsafe code?

Unsafe code can be used in Visual Studio IDE by following the given steps: Double click properties in the Solution Explorer to open project properties. Click the Build tab and select the option “Allow Unsafe Code”.

How do you compile with unsafe?

For compiling unsafe code, you have to specify the /unsafe command-line switch with command-line compiler. Open Project properties by double clicking the properties node in the Solution Explorer. Click on the “Build” tab. Select the option "Allow unsafe code".

Why do we use unsafe in C#?

C# supports an unsafe context, in which you may write unverifiable code. In an unsafe context, code may use pointers, allocate and free blocks of memory, and call methods using function pointers. Unsafe code in C# isn't necessarily dangerous; it's just code whose safety cannot be verified.

What is the use unsafe keyword?

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.


2 Answers

It's very simple to publish with a unsafe code

Here is how you do it: enter image description here

like image 112
hackp0int Avatar answered Oct 19 '22 22:10

hackp0int


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.

like image 43
Wolf5 Avatar answered Oct 19 '22 22:10

Wolf5