发布带有不安全代码的 Web 应用程序

7
我正在尝试发布一个Web应用程序(使用VS2012 Web),其中需要运行VB脚本。由于权限不足,该脚本目前无法正确运行。我当前正在尝试以用户身份运行它并提供一些凭据。我必须提供的密码必须在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();
3个回答

9

使用不安全的代码进行发布非常简单。

以下是操作方法: enter image description here


3
谢谢你的回答,但那个标志只是告诉C#编译器去编译不安全的代码... 对我有用的解决方案就是删除不安全的代码,并放弃在C#中显式使用指针 :) - Bun
所有配置和允许安全代码都应该被选中。 - Rahamath

6

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

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

这对我解决了问题。

我在谷歌上搜索了这个问题,但是没有找到任何有用的信息。我检查了*.targets文件中是否有"unsafe",这使我找到了"AllowUnsafeBlocks"标签。


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>

不,同样的问题...我不得不将它包含在<system.codedom>标签中才能编译,但它仍然无法发布...奇怪的是,我会在大约1秒钟内收到编译器错误(CS0227),然后它就消失了。 - Bun
我也有同样的问题。有没有什么解决办法可以防止它发生? - Karthik Dheeraj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接