2013-08-07 106 views
3

我已經下載了通過https提供API的產品的試用版。目前我的asp.net mvc的web應用程序中我做了以下執行的API調用該產品的試用版: -Security.Authentication.AuthenticationException:根據驗證程序,遠程證書無效

[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在 System.Net.Security.SslState(ProtocolToken 消息,AsyncProtocolRequest asyncRequest,例外的例外)在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken 消息,AsyncProtocolRequest asyncRequest) .StartSendBlob(字節[]來電,的Int32 計數,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ProcessReceivedBlob(字節[]緩衝液,的Int32 計數,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReadFrame (Byte [] buffer,Int32 readBytes,AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.StartReceiveBlob(字節[]緩衝液, AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken 消息,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字節[]來電,的Int32 計數,AsyncProtocolRequest asyncRequest)處 System.Net.Security.SslState.StartReadFrame(字節[]緩衝 System.Net.Security.SslState.ProcessReceivedBlob(字節[]緩衝液,的Int32 計數,AsyncProtocolRequest asyncRequest)的Int32 的ReadBytes,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartReceiveBlob(Byte [] buffer, AsyncProtocolRequest asyncRequest)at S ystem.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken 消息,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 (Byte [] buffer, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message,As yncProtocolRequest asyncRequest)在 System.Net.Security.SslState.StartSendBlob(字節[]來電,的Int32 計數,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ForceAuthentication(布爾receiveFirst, 字節[]緩衝液,AsyncProtocolRequest asyncRequest )在在 System.Threading.ExecutionContext.RunInternal(ExecutionContext中 的ExecutionContext在System.Net.TlsStream.CallProcessAuthentication System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)(對象 狀態),ContextCallback回調,對象的狀態,布爾型 preserveSyncCtx) 系統。Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback回調,對象的狀態,布爾 preserveSyncCtx)在 System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback回調,對象狀態)在 System.Net.TlsStream.ProcessAuthentication( LazyAsyncResult結果) System.Net.TlsStream.Write(Byte [] buffer,Int32 offset,Int32 size)at System.Net.PooledStream.Write(Byte [] buffer,Int32 offset,Int32 size) at System.Net .ConnectStream.WriteHeaders(布爾異步)

巴林在米IND,我可以調用API沒有任何問題,如果我直接從我的Web瀏覽器中調用的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,而且因爲我在我的開發機器內部使用產品的試用版時,是否應該安裝有效的證書?或者問題與安全認證無關? 謝謝

回答

24

問題是您尚未配置有效的SSL證書。由於有效的SSL證書花費金錢,爲了開發目的,我猜你使用了自動簽名證書。您可以通過設置忽略這些錯誤ServerCertificateValidationCallback靜態屬性在Application_Start

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

注意:避免在生產中這樣做。在LIVE上部署時,請確保您使用的是有效的SSL證書。

+0

非常感謝它運作良好,並確保生產我將使用有效的證書。 –

+0

我得到同樣的錯誤,而從控制檯應用程序連接smtp服務器,在主函數的開始使用代碼,它解決了問題....非常感謝你分享它。 –

0

隨着點頭達林季米特洛夫... 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 
相關問題