Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止ASP.NET上不安全代码的System.Security.VerificationException?_C#_Asp.net_Asp.net Web Api_Mailkit_Mimekit - Fatal编程技术网

C# 如何防止ASP.NET上不安全代码的System.Security.VerificationException?

C# 如何防止ASP.NET上不安全代码的System.Security.VerificationException?,c#,asp.net,asp.net-web-api,mailkit,mimekit,C#,Asp.net,Asp.net Web Api,Mailkit,Mimekit,我正在开发一个基于.NET Framework 4.7的ASP.NET Web API 2.0应用程序,在尝试使用MailKit发送电子邮件时遇到此错误。在本地,我的应用运行良好,但当我在基于Windows Server 2012的主机中发布时,我收到了MikeKit.ParserOptions异常,我无法发送电子邮件。 这里是我的邮差错误: { "Message": "An error has occurred.", "ExceptionMessage": "The type

我正在开发一个基于.NET Framework 4.7的ASP.NET Web API 2.0应用程序,在尝试使用MailKit发送电子邮件时遇到此错误。在本地,我的应用运行良好,但当我在基于Windows Server 2012的主机中发布时,我收到了MikeKit.ParserOptions异常,我无法发送电子邮件。 这里是我的邮差错误:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The type initializer for 'MimeKit.ParserOptions' threw an exception.",
    "ExceptionType": "System.TypeInitializationException",
    "StackTrace": "   at MimeKit.MimeMessage..ctor()\r\n   at AgriturismoIlMelogranoNet.Controllers.EmailRequestController.Post(EmailRequest emailRequest) in C:\\Users\\magar\\source\\repos\\AgriturismoIlMelogranoNet\\AgriturismoIlMelogranoNet\\Controllers\\EmailRequestController.cs:line 20\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "The type initializer for 'MimeKit.Utils.CharsetUtils' threw an exception.",
        "ExceptionType": "System.TypeInitializationException",
        "StackTrace": "   at MimeKit.ParserOptions..ctor()\r\n   at MimeKit.ParserOptions..cctor()",
        "InnerException": {
            "Message": "An error has occurred.",
            "ExceptionMessage": "Operation could destabilize the runtime.",
            "ExceptionType": "System.Security.VerificationException",
            "StackTrace": "   at MimeKit.Utils.OptimizedOrdinalIgnoreCaseComparer.GetHashCode(String obj)\r\n   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\r\n   at MimeKit.Utils.CharsetUtils.AddAliases(Dictionary`2 dict, Int32 codepage, Int32 fallback, String[] names)\r\n   at MimeKit.Utils.CharsetUtils..cctor()"
        }
    }
}
有什么想法吗?我的主机提供商il叫阿鲁巴。
谢谢

MimeKit使用了
不安全的
代码,您的ASP.NET web服务未配置为允许这样做。也许有人可以对此评论进行扩展,为您提供一个实际的解决方案。那么我是否需要联系托管服务?我不知道,因为我不编写ASP.NET应用程序。我更改了您的标题,以便知道答案的人可能会发现此问题。应用程序的信任级别需要提升到完全信任级别。我想你的主人需要处理这件事。MimeKit使用
不安全的
代码,您的ASP.NET web服务未配置为允许这样做。也许有人可以对此评论进行扩展,为您提供一个实际的解决方案。那么我是否需要联系托管服务?我不知道,因为我不编写ASP.NET应用程序。我更改了您的标题,以便知道答案的人可能会发现此问题。应用程序的信任级别需要提升到完全信任级别。我想你的主人需要处理这件事。
  <location path="site.it" allowOverride="true">
    <system.web>
      <trust level="Full" originUrl="" />
      <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
    </system.web>
  </location>
public IHttpActionResult Post([FromBody] EmailRequest emailRequest)
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Sistema automatico", Username));
    message.To.Add(new MailboxAddress(To));
    message.Subject = "Richiesta da parte del cliente " + emailRequest.Name;

    var bodyBuilder = new BodyBuilder();
    bodyBuilder.TextBody = emailRequest.Message;
    message.Body = bodyBuilder.ToMessageBody();

    using (var client = new SmtpClient(/*new ProtocolLogger(Console.OpenStandardOutput())*/))
    {
        // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
        client.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
        client.Connect(Host, Port, SecureSocketOptions.Auto);

        // Note: only needed if the SMTP server requires authentication
        client.Authenticate(Username, Password);
        try
        {
            client.Send(message);
            return Ok();
        }
        catch (Exception)
        {
            return BadRequest();
        }
        finally
        {
            client.Disconnect(true);
        }
    }
}