{"id":2334737,"username":"earthcrosser","avatar_template":"/user_avatar/discussions.unity.com/earthcrosser/{size}/581864_2.png","created_at":"2014-08-18T12:31:49.000Z","cooked":"\u003cp\u003eAs promised here’s what my final solution looks like\u003c/p\u003e\n\u003cpre data-code-wrap=\"csharp\"\u003e\u003ccode class=\"lang-csharp\"\u003eusing UnityEngine;\nusing System.Collections;\nusing System.IO;\nusing System.Text;\nusing System.Net;\nusing System.Net.Security;\nusing System.Security.Cryptography.X509Certificates;\nusing System.Xml;\n\npublic class RestCalls : MonoBehaviour\n{\n       \n        X509Certificate2 adminClient;\n        string clientCertPath=\"\";\n        static string baseUrl = \"https://\"someurl\"/rest/\";\n        void Start ()\n        {\n                clientCertPath = Application.persistentDataPath + \"/adminClient.p12\";\n                StartCoroutine (GetCert ());   \n        }\n\n        private IEnumerator GetCert ()\n        {\n                if (File.Exists (clientCertPath)) {\n                        print (\"I have the file\");\n                        MakeRestCall();\n                } else {\n                        WWW download = new WWW (\"http://\"pathtocert\"/adminClient.p12\");\n                        yield return download;\n                        if (download.error != null) {\n                                print (\"Error downloading: \" + download.error);\n                        } else {\n                                File.WriteAllBytes (clientCertPath, download.bytes);\n                               \n                        }\n                }\n        }\n\n        public XmlDocument MakeRestCall()\n        {\n            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback (ValidateServerCertificate);\n            adminClient = new X509Certificate2 (clientCertPath, \"xxxxx\");\n            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (baseUrl + \"user-message\");\n            request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;\n           \n       \n            request.ClientCertificates.Add (adminClient);\n            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();\n           \n            XmlDocument xmlDoc = new XmlDocument ();\n            xmlDoc.Load (response.GetResponseStream ());\n\n            return xmlDoc;\n           \n            \n        }\n\n        public static bool ValidateServerCertificate (\n            object sender,\n            System.Security.Cryptography.X509Certificates.X509Certificate certificate,\n            X509Chain chain,\n            System.Net.Security.SslPolicyErrors sslPolicyErrors)\n        {\n           //TODO Make this more secure\n            return true;\n        }\n\n       \n}\n\u003c/code\u003e\u003c/pre\u003e","post_number":18,"post_type":1,"posts_count":22,"updated_at":"2014-08-18T12:31:49.000Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"incoming_link_count":10,"reads":0,"readers_count":0,"score":50.0,"yours":false,"topic_id":525675,"topic_slug":"ssl-certificate-validation","primary_group_name":null,"flair_name":null,"flair_url":null,"flair_bg_color":null,"flair_color":null,"flair_group_id":null,"badges_granted":[],"version":1,"can_edit":false,"can_delete":false,"can_recover":false,"can_see_hidden_post":false,"can_wiki":false,"user_title":null,"bookmarked":false,"raw":"As promised here's what my final solution looks like \n\n```csharp\nusing UnityEngine;\nusing System.Collections;\nusing System.IO;\nusing System.Text;\nusing System.Net;\nusing System.Net.Security;\nusing System.Security.Cryptography.X509Certificates;\nusing System.Xml;\n\npublic class RestCalls : MonoBehaviour\n{\n       \n        X509Certificate2 adminClient;\n        string clientCertPath=\"\";\n        static string baseUrl = \"https://\"someurl\"/rest/\";\n        void Start ()\n        {\n                clientCertPath = Application.persistentDataPath + \"/adminClient.p12\";\n                StartCoroutine (GetCert ());   \n        }\n\n        private IEnumerator GetCert ()\n        {\n                if (File.Exists (clientCertPath)) {\n                        print (\"I have the file\");\n                        MakeRestCall();\n                } else {\n                        WWW download = new WWW (\"http://\"pathtocert\"/adminClient.p12\");\n                        yield return download;\n                        if (download.error != null) {\n                                print (\"Error downloading: \" + download.error);\n                        } else {\n                                File.WriteAllBytes (clientCertPath, download.bytes);\n                               \n                        }\n                }\n        }\n\n        public XmlDocument MakeRestCall()\n        {\n            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback (ValidateServerCertificate);\n            adminClient = new X509Certificate2 (clientCertPath, \"xxxxx\");\n            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (baseUrl + \"user-message\");\n            request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;\n           \n       \n            request.ClientCertificates.Add (adminClient);\n            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();\n           \n            XmlDocument xmlDoc = new XmlDocument ();\n            xmlDoc.Load (response.GetResponseStream ());\n\n            return xmlDoc;\n           \n            \n        }\n\n        public static bool ValidateServerCertificate (\n            object sender,\n            System.Security.Cryptography.X509Certificates.X509Certificate certificate,\n            X509Chain chain,\n            System.Net.Security.SslPolicyErrors sslPolicyErrors)\n        {\n           //TODO Make this more secure\n            return true;\n        }\n\n       \n}\n```","actions_summary":[{"id":2,"count":2}],"moderator":false,"admin":false,"staff":false,"user_id":13728,"hidden":false,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"post_url":"/t/ssl-certificate-validation/525675/18","reactions":[{"id":"+1","type":"emoji","count":2}],"current_user_reaction":null,"reaction_users_count":2,"current_user_used_main_reaction":false,"user_signature":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false,"topic_accepted_answer":null}