示例#1
0
        /// <summary>
        /// Checks credentials against the service. Useful to verify validity of credentials before storing them for later use.
        /// </summary>
        /// <param name="firstTitleCredential">Ther First Title credential to check against the First Title service.</param>
        /// <returns><c>true</c> if the credentials passed the authentication check, otherwise <c>false</c>.</returns>
        /// <exception cref="HttpRequestException">If the check failed due to an infrastructure issue.</exception>
        public async Task <bool> CheckCredentials(FirstTitleCredential credential)
        {
            if (credential is null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            if (string.IsNullOrEmpty(credential.Username) || string.IsNullOrEmpty(credential.Password))
            {
                return(false);
            }

            // Some sanity checks. 10,000 is arbitrary. High enough to hopefully never be encountered for
            // a legitimate username/password and low enough to reduce side effects of attacks.
            if (credential.Username.Length > 10000 || credential.Password.Length > 10000)
            {
                return(false);
            }

            HttpResponseMessage response;

            using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, ServiceUri))
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", GetBasicAuthValue(credential));
                response = await _httpClient.SendAsync(requestMessage);
            }

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                return(false);
            }

            // This will throw an exception with the relevant details so that
            // the caller knows that the check wasn't able to be completed.
            response.EnsureSuccessStatusCode();

            // If it didn't throw, then we know we had a successful return code
            // so the credentials must be valid.
            return(true);
        }
 public InvalidFirstTitleCredentialsException(FirstTitleCredential firstTitleCredential)
 {
     FirstTitleCredential = firstTitleCredential;
 }
示例#3
0
        private static string GetBasicAuthValue(FirstTitleCredential firstTitleCredential)
        {
            var credentialsByteArray = Encoding.ASCII.GetBytes($"{firstTitleCredential.Username}:{firstTitleCredential.Password}");

            return(Convert.ToBase64String(credentialsByteArray));
        }
 public InvalidFirstTitleCredentialsException(string message, FirstTitleCredential firstTitleCredential) : base(message)
 {
     FirstTitleCredential = firstTitleCredential;
 }