コード例 #1
0
        /// <summary>
        /// Create a WebRequest for the specified path.
        /// </summary>
        /// <param name="path">Path, NOT including the leading slash.</param>
        /// <returns>A WebRequest for the specified appliance path.</returns>
        public WebRequest Request(string path)
        {
            WebRequest reqest = WebRequest.Create(this.ApplianceUri + path);

            reqest.Timeout = ApiConfigurationManager.GetInstance().HttpRequestTimeout;
            return(reqest);
        }
コード例 #2
0
        /// <summary>
        /// Obtain a Conjur authentication token.
        /// </summary>
        /// <returns>Conjur authentication token in verbatim form.
        /// It needs to be base64-encoded to be used in a web request.</returns>
        public string GetToken()
        {
            string token = this.token;

            if (token != null)
            {
                return(token);
            }

            lock (this.locker)
            {
                if (this.token == null)
                {
                    HttpWebRequest request = WebRequest.CreateHttp(this.uri);
                    request.Timeout                   = ApiConfigurationManager.GetInstance().HttpRequestTimeout;
                    request.Method                    = WebRequestMethods.Http.Post;
                    request.ContentLength             = credential.SecurePassword.Length;
                    request.AllowWriteStreamBuffering = false;

                    IntPtr bstr = IntPtr.Zero;
                    byte[] bArr = new byte[credential.SecurePassword.Length];
                    try
                    {
                        bstr = Marshal.SecureStringToBSTR(credential.SecurePassword);
                        for (int i = 0; i < credential.SecurePassword.Length; i++)
                        {
                            bArr[i] = Marshal.ReadByte(bstr, i * 2);
                        }
                        using (Stream stream = request.GetRequestStream())
                        {
                            stream.Write(bArr, 0, bArr.Length);
                            Interlocked.Exchange(ref this.token, request.Read());
                            this.StartTokenTimer(TimeSpan.FromMilliseconds(ApiConfigurationManager.GetInstance().TokenRefreshTimeout));
                        }
                    }
                    finally
                    {
                        if (bstr != IntPtr.Zero)
                        {
                            Marshal.ZeroFreeBSTR(bstr);
                        }
                        Array.Clear(bArr, 0, bArr.Length);
                    }
                }
            }
            return(this.token);
        }