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

时间:2013-05-15 14:02:36

标签: c# asp.net publishing unsafe

我正在尝试发布一个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 个答案:

答案 0 :(得分:7)

使用不安全的代码

发布非常简单

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

答案 1 :(得分:4)

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

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

这解决了我。

用Google搜索此问题但找不到任何内容。检查了* .targets文件中的&#34; unsafe&#34;这让我进入了#34; AllowUnsafeBlocks&#34;标签

答案 2 :(得分: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>
相关问题