title: Certificate Invalid Issue with Alamofire 4.0
body: <p>I am trying to consume web services for my iOS app over https. The web server uses a self signed certificate.</p>
  
  <p>When consuming the web service, I get the error “certificate is Invalid”.</p>
  
  <blockquote>
    <p>FAILURE: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “portal” which could put your confidential information at risk." </p>
  </blockquote>
  
  <p>I know the best practise is to fix this at the server side to enable a trusted root CA. But as this is a temporary development environment, we are using a self signed certificate.
  Since this is ATS issue, I have edited ATS in my info.plist as below.</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
  &lt;plist version="1.0"&gt;
  &lt;dict&gt;
      &lt;key&gt;NSExceptionDomains&lt;/key&gt;
      &lt;dict&gt;
          &lt;key&gt;devportal&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;NSTemporaryExceptionMinimumTLSVersion&lt;/key&gt;
              &lt;string&gt;TLSv1.2&lt;/string&gt;
              &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSExceptionRequiresForwardSecrecy&lt;/key&gt;
              &lt;false/&gt;
              &lt;key&gt;NSExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
              &lt;true/&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
      &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
      &lt;false/&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>As the NSException domains doesn’t work with IP and port number, I have created a host entry in my etc/hosts file for the web server IP and consuming it like <a href="https://devportal:8443/rest/login" rel="noreferrer">https://devportal:8443/rest/login</a> instead of consuming it as <a href="https://192.22.xx.xxx:8443/rest/login" rel="noreferrer">https://192.22.xx.xxx:8443/rest/login</a> </p>
  
  <p>I have followed alamofire documentation on server trust policies, edited ATS to allow exception domains but nothing worked out for me. I have spent over 3 days on this issue. Am I missing something? Does anybody faced a similar issue? Is there any solution for this? Thanks in advance</p>
  
  <p>I am using almofire 4.0, Xcode 8.0. Below is my code.</p>
  
  <pre><code>class LoginService{
       private static var Manager: Alamofire.SessionManager = {
  
            let pathToCert = Bundle.main.path(forResource: "192.22.xx.xxx", ofType: "crt") // Downloaded this certificate and have added to my bundle
            let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
  
            // Create the server trust policies
            let serverTrustPolicies: [String: ServerTrustPolicy] = [
                 "192.22.xx.xxx": .pinCertificates(
                      certificates: [SecCertificateCreateWithData(nil, localCertificate)!],
                      validateCertificateChain: true,
                      validateHost: true
                 ),
  
                 "devportal:8443": .disableEvaluation
            ]
  
            // Create custom manager
            let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
            let manager = Alamofire.SessionManager(
                 configuration: URLSessionConfiguration.default,
                 serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
            )
  
            return manager
       }()
  
  
  
       /**
        Calls the Login Web Service to authenticate the user
        */
       public func login(username:String, password: String){
  
            let parameters = [
                 "username": "TEST",
                 "password": "PASSWORD",
                    ]
            let header: HTTPHeaders = ["Accept": "application/json"]
            LoginService.Manager.request("https://devportal:8443/rest/login", method: .post, parameters: parameters, encoding: JSONEncoding(options: []),headers :header).responseJSON { response in
                 debugPrint(response)
  
                 if let json = response.result.value {
                      print("JSON: \(json)")
                 }
            }
  
  
  
       }
  }
  </code></pre>