【问题标题】:Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedureSecurity.Authentication.AuthenticationException:根据验证程序,远程证书无效
【发布时间】:2013-08-08 18:24:05
【问题描述】:

我已经下载了通过 https 提供 API 的产品的试用版。目前在我的 asp.net mvc Web 应用程序中,我正在执行以下操作来执行对产品试用版的 API 调用:-

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(RackJoin rj, FormCollection formValues)
        {
            string controllername = RouteData.Values["controller"].ToString();
            if (ModelState.IsValid)
            {
                var message = "";
                var status = "";
                long assetid = new long();
                XmlDocument doc = new XmlDocument();
                using (var client = new WebClient())
                {
                    var query = HttpUtility.ParseQueryString(string.Empty);
                    foreach (string key in formValues)
                    {
                        query[key] = this.Request.Form[key];
                    }

query["username"] =  System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];
query["password"] =  System.Web.Configuration.WebConfigurationManager.AppSettings["ApiPassword"];
string apiurl = "https://win-spdev:8400/servlets/AssetServlet";
//code goes here…..
                    var url = new UriBuilder(apiurl);
                    url.Query = query.ToString();

但我会得到以下异常:-

发生错误:System.Security.Authentication.AuthenticationException: 远程证书根据验证无效 程序。在 System.Net.Security.SslState.StartSendAuthResetSignal(协议令牌 消息,AsyncProtocolRequest asyncRequest,异常异常)在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(协议令牌 消息,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字节 [] 传入,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ProcessReceivedBlob(字节 [] 缓冲区,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReadFrame(字节 [] 缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReceiveBlob(字节 [] 缓冲区, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(协议令牌 消息,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字节 [] 传入,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ProcessReceivedBlob(字节 [] 缓冲区,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReadFrame(字节 [] 缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReceiveBlob(字节 [] 缓冲区, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(协议令牌 消息,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字节 [] 传入,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ProcessReceivedBlob(字节 [] 缓冲区,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReadFrame(字节 [] 缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReceiveBlob(字节 [] 缓冲区, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(协议令牌 消息,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字节 [] 传入,Int32 计数,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ForceAuthentication(布尔接收第一, Byte[] 缓冲区,AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) 在 System.Net.TlsStream.CallProcessAuthentication(Object 州)在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult 结果)在 System.Net.TlsStream.Write(Byte[] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.PooledStream.Write(Byte[] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.ConnectStream.WriteHeaders(布尔异步)

请记住,如果我直接从我的网络浏览器调用 API,我可以毫无问题地调用 API,如下所示:-

https://win-spdev:8400/servlets/AssetServlet?username=tmsservice&password=test2test2&assetType=Rack&operation=UpdateAsset&assetName=rack9090909858585&accountName=GROUP&siteName=test%20site&productName=rackproductone

那么这个问题是否与 API 通过 https 相关,并且由于我在我的开发机器中使用产品的试用版,我应该安装有效的证书吗?还是问题与安全认证无关? 谢谢

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    问题是您没有配置有效的 SSL 证书。由于有效的 SSL 证书需要花钱,出于开发目的,我猜您使用了自动签名证书。您可以通过在 Application_Start 中设置 ServerCertificateValidationCallback 静态属性来忽略这些错误:

    System.Net.ServicePointManager.ServerCertificateValidationCallback += 
        (s, cert, chain, sslPolicyErrors) => true;
    

    注意:避免在生产环境中这样做。当您在 LIVE 上部署时,请确保您使用的是有效的 SSL 证书。

    【讨论】:

    • 非常感谢它运行良好,并且确保生产我将使用有效的证书。
    • 我在从控制台应用程序连接 smtp 服务器时遇到了同样的错误,使用了 main 函数开头的代码,它解决了问题....非常感谢您的分享。
    • 当我使用代码时,我的应用程序抛出了一个异常,其根本原因是(在这种情况下,它是由于错误的 DNS 声明导致 DNS 身份检查失败),谢谢。
    【解决方案2】:

    向 Darin Dimitrov 致敬……VB.NET 的人们可以使用它(在我的 Web 应用程序的 Global.asax.vb 中):

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    System.Net.ServicePointManager.ServerCertificateValidationCallback = Function(s, cert, chain, sslPolicyErrors)
                                                                           Return True
                                                                         End Function
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2016-11-28
      • 2012-07-20
      • 2015-03-17
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多