예제 #1
0
        public string ToString(SignatureGrouping grouping)
        {
            string result = Value.ToString(SignatureGrouping.None);

            return(grouping == SignatureGrouping.Parentheses
                ? result.Enclose("(", ")")
                : result.Enclose("[", "]"));
        }
        /// <summary>
        /// 使用请求拦截器,为每个请求自动添加基于 https://tools.ietf.org/html/draft-cavage-http-signatures 的签名。
        /// </summary>
        /// <param name="httpClientBuilder"></param>
        /// <param name="signatureGenerator"></param>
        /// <param name="parameterGenerator"></param>
        /// <returns></returns>
        public static IHttpClientBuilder UseHttpSignatureInterceptor(this IHttpClientBuilder httpClientBuilder, ISignatureGenerator signatureGenerator, Func <IHttpRequest, ISigningParameter> parameterGenerator)
        {
            if (signatureGenerator == null)
            {
                throw new ArgumentNullException(nameof(signatureGenerator));
            }
            if (parameterGenerator == null)
            {
                throw new ArgumentNullException(nameof(parameterGenerator));
            }

            return(httpClientBuilder.AddRequestInterceptor(request =>
            {
                ISigningParameter signingParameter = parameterGenerator.Invoke(request);
                ISignature signature = signatureGenerator.Sign(request.ToSigningRequest(), signingParameter);
                request.AddHeader("Signature", signature.ToString(SignatureFormatting.Signature));
                request.AddHeader("Authorization", signature.ToString(SignatureFormatting.Authorization));
            }));
        }