Example #1
0
        private async Task <bool> TryAuthenticate(HttpClient client, NssConnectionState nssConnection)
        {
            try
            {
                if (nssConnection == null)
                {
                    return(false);
                }

                //Special case here, does not go through the Send methods
                var httpResponseMessage = await client.PostAsync("auth/token", new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", "password"),
                    new KeyValuePair <string, string>("username", nssConnection.Username),
                    new KeyValuePair <string, string>("password", nssConnection.Password)
                }));

                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var loginResponse = await httpResponseMessage.ContentFromJsonAsync <LoginResponse>();

                    nssConnection.AccessToken             = loginResponse.AccessToken;
                    nssConnection.RetryAuthenticateFailed = false;
                    await this.clientCredentialStore.SetAsync(nssConnection);

                    return(true);
                }
                else
                {
                    nssConnection.RetryAuthenticateFailed = true;
                    await this.clientCredentialStore.SetAsync(nssConnection);
                }
            }
            catch { }

            return(false);
        }
 public Task Clear()
 {
     this.nssConnectionState = null;
     return(Task.CompletedTask);
 }
 public Task SetAsync(NssConnectionState nssConnectionState)
 {
     this.nssConnectionState = nssConnectionState;
     return(Task.CompletedTask);
 }
Example #4
0
 public HttpClient Create(NssConnectionState nssConnectionState) => Create(nssConnectionState.BaseUrl);