簡體   English   中英

在Mono上使用自定義SSL客戶端證書System.Net.HttpClient

[英]Using custom SSL client certificates System.Net.HttpClient on Mono

我正在使用NuGet的Microsoft HTTP客戶端庫 ,我基本上嘗試使用X509Certificate2證書在HttpClient中允許TLS身份驗證。

我試過像這樣創建客戶端:

WebRequestHandler certHandler = new WebRequestHandler () {
    ClientCertificateOptions = ClientCertificateOption.Manual,
    UseDefaultCredentials = false
};
certHandler.ClientCertificates.Add (this.ClientCertificate);
HttpClient client = new HttpClient (certHandler);

但是, certHandler.ClientCertificates失敗,因為此getter 未在Mono中實現 ,因此我從中獲得了NotImplementedException。 (我不確定為什么那仍然是TODO。)

到目前為止,我運氣不好。 任何想法如何在Mono環境中簡單地在HttpClient上設置客戶端證書?

它看起來好像只是能夠將客戶端證書集合傳遞給CreateWebRequest中的HttpWebRequest,因此,由於繼承不起作用,我只是從mono復制/粘貼該類並添加了實現。

// Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Net.Cache;
using System.Net.Security;
using System.Security.Principal;
using System.Security.Cryptography.X509Certificates;

namespace System.Net.Http
{
    public class WebRequestHandlerWithClientcertificates : HttpClientHandler
    {
        bool allowPipelining;
        RequestCachePolicy cachePolicy;
        AuthenticationLevel authenticationLevel;
        TimeSpan continueTimeout;
        TokenImpersonationLevel impersonationLevel;
        int maxResponseHeadersLength;
        int readWriteTimeout;
        RemoteCertificateValidationCallback serverCertificateValidationCallback;
        bool unsafeAuthenticatedConnectionSharing;
        private X509CertificateCollection clientCertificates;

        public WebRequestHandler ()
        {
            allowPipelining = true;
            authenticationLevel = AuthenticationLevel.MutualAuthRequested;
            cachePolicy = System.Net.WebRequest.DefaultCachePolicy;
            continueTimeout = TimeSpan.FromMilliseconds (350);
            impersonationLevel = TokenImpersonationLevel.Delegation;
            maxResponseHeadersLength = HttpWebRequest.DefaultMaximumResponseHeadersLength;
            readWriteTimeout = 300000;
            serverCertificateValidationCallback = null;
            unsafeAuthenticatedConnectionSharing = false;
        }

        public bool AllowPipelining {
            get { return allowPipelining; }
            set {
                EnsureModifiability ();
                allowPipelining = value;
            }
        }

        public RequestCachePolicy CachePolicy {
            get { return cachePolicy; }
            set {
                EnsureModifiability ();
                cachePolicy = value;
            }
        }

        public AuthenticationLevel AuthenticationLevel {
            get { return authenticationLevel; }
            set {
                EnsureModifiability ();
                authenticationLevel = value;
            }
        }

        public X509CertificateCollection ClientCertificates {
            get {
                if (clientCertificates==null) {
                    clientCertificates = new X509CertificateCollection();
                }
                return clientCertificates;
            }
            set {
                if (value==null) {
                    throw new ArgumentNullException("value");
                }
                EnsureModifiability ();
                clientCertificates = value;
            }
        }

        [MonoTODO]
        public TimeSpan ContinueTimeout {
            get { return continueTimeout; }
            set {
                EnsureModifiability ();
                continueTimeout = value;
            }
        }

        public TokenImpersonationLevel ImpersonationLevel {
            get { return impersonationLevel; }
            set {
                EnsureModifiability ();
                impersonationLevel = value;
            }
        }

        public int MaxResponseHeadersLength {
            get { return maxResponseHeadersLength; }
            set {
                EnsureModifiability ();
                maxResponseHeadersLength = value;
            }
        }

        public int ReadWriteTimeout {
            get { return readWriteTimeout; }
            set {
                EnsureModifiability ();
                readWriteTimeout = value;
            }
        }

        [MonoTODO]
        public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
            get { return serverCertificateValidationCallback; }
            set {
                EnsureModifiability ();
                serverCertificateValidationCallback = value;
            }
        }

        public bool UnsafeAuthenticatedConnectionSharing {
            get { return unsafeAuthenticatedConnectionSharing; }
            set {
                EnsureModifiability ();
                unsafeAuthenticatedConnectionSharing = value;
            }
        }

        internal override HttpWebRequest CreateWebRequest (HttpRequestMessage request)
        {
            HttpWebRequest wr = base.CreateWebRequest (request);

            wr.Pipelined = allowPipelining;
            wr.AuthenticationLevel = authenticationLevel;
            wr.CachePolicy = cachePolicy;
            wr.ImpersonationLevel = impersonationLevel;
            wr.MaximumResponseHeadersLength = maxResponseHeadersLength;
            wr.ReadWriteTimeout = readWriteTimeout;
            wr.UnsafeAuthenticatedConnectionSharing = unsafeAuthenticatedConnectionSharing;
            // here : maybe wr.ClientCertificates = ClientCertificates if the line below throws an error in your tests
            wr.ClientCertificates.Add(ClientCertificates);

            return wr;
        }
    }
}

現在在您的代碼中使用WebRequestHandlerWithClientCertificates而不是WebRequestHandler

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM