7

我正在尝试发布一个需要在其中运行 vb 脚本的 Web 应用程序(使用 VS2012 Web)。

该脚本当前无法正确运行,可能是因为缺少权限。我目前正在尝试以用户身份运行它并提供一些凭据。我必须提供的密码必须在一个System.Security.SecureString需要创建 char* 的地方。

当我在调试中运行我的应用程序时,一切正常且符合预期。但是到了将应用程序发布到服务器的时候,它会说:

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

我已经在项目属性中允许了不安全的代码,但我不知道为什么 VS 在调试时看到该属性而不是在发布时看到该属性。

当我点击发布时,我在错误列表中看到了 CS0227 错误,但它只停留了 1 秒钟,然后消失了……似乎那一秒钟足以使构建失败。

关于问题是什么的任何想法?

这是我的代码:

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();
4

3 回答 3

9

It's very simple to publish with a unsafe code

Here is how you do it: enter image description here

于 2013-10-29T16:04:36.490 回答
6

发布时似乎忽略了不安全的项目设置,因此您需要手动打开 .CSPROJ 文件并包含:

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

这为我解决了它。

谷歌搜索了这个问题,但找不到任何东西。检查 *.targets 文件中的“不安全”,这导致我找到“AllowUnsafeBlocks”标签。

于 2015-09-26T23:28:13.617 回答
2

请将此代码添加到 Web.config 文件中并检查它是否适合您

<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>
于 2013-05-15T14:26:29.070 回答