From b0416561b11752678b8cbd891b5e00c1c9907a4d Mon Sep 17 00:00:00 2001 From: Julien Patte Date: Wed, 17 Dec 2014 13:52:15 +0100 Subject: [PATCH] avoid rebuilding the Basic Authentication header for each request The username and password never change, so there is no need to re-encode them in Base64 for each request. We can juste compute the header value once and reuse it to save time. --- RestSharp/Authenticators/HttpBasicAuthenticator.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/RestSharp/Authenticators/HttpBasicAuthenticator.cs b/RestSharp/Authenticators/HttpBasicAuthenticator.cs index 87cd378da..0b254c517 100644 --- a/RestSharp/Authenticators/HttpBasicAuthenticator.cs +++ b/RestSharp/Authenticators/HttpBasicAuthenticator.cs @@ -22,13 +22,12 @@ namespace RestSharp { public class HttpBasicAuthenticator : IAuthenticator { - private readonly string _username; - private readonly string _password; + private readonly string _authHeader; public HttpBasicAuthenticator(string username, string password) { - _password = password; - _username = username; + var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username, password))); + _authHeader = string.Format("Basic {0}", token); } public void Authenticate(IRestClient client, IRestRequest request) @@ -43,10 +42,7 @@ public void Authenticate(IRestClient client, IRestRequest request) // only add the Authorization parameter if it hasn't been added by a previous Execute if (!request.Parameters.Any(p => p.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase))) { - var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _username, _password))); - var authHeader = string.Format("Basic {0}", token); - - request.AddParameter("Authorization", authHeader, ParameterType.HttpHeader); + request.AddParameter("Authorization", _authHeader, ParameterType.HttpHeader); } } }