Пример #1
0
 public async Task<AsyncResult> CreateUser(User user)
 {
     var result = new AsyncResult();
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var data = JsonConvert.SerializeObject(user);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/users", UriKind.Relative);
             using (var resp = await client.PostAsync(uri, content))
             {
                 if (resp.StatusCode == HttpStatusCode.Created)
                 {
                     result.Succeed = true;
                     return result;
                 }
                 if (resp.StatusCode == HttpStatusCode.InternalServerError)
                 {
                     result.ErrorMessage = "Произошла ошибка на сервере";
                 }
                 else if (resp.StatusCode == HttpStatusCode.BadRequest)
                 {
                     var err = await resp.Content.ReadAsStringAsync();
                     result.ErrorMessage = TranslateError(err);
                 }
                 return result;
             }
         }
     }
 }
Пример #2
0
        public async Task<AsyncResult> Login(string login, string password)
        {
            var result = new AsyncResult();
            using (var client = new BaseHttpClient())
            {
                var data = new Dictionary<string, string>
                {
                    {"grant_type", "password"},
                    {"username", login},
                    {"password", password},
                };

                using (var content = new FormUrlEncodedContent(data))
                using (var resp = await client.PostAsync("/Token", content))
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        using (var stream = await resp.Content.ReadAsStreamAsync())
                        using (var streamReader = new StreamReader(stream))
                        using (var jsonReader = new JsonTextReader(streamReader))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            var token = serializer.Deserialize<AuthToken>(jsonReader);
                            if (token != null)
                            {
                                store.SaveToken(token);
                                result.Succeed = true;
                            }
                        }
                    }
                }
            }
            return result;
        }
Пример #3
0
        public async Task<AsyncResult> ChangePasswordAsync(string login, string oldPassword, string newPassword)
        {
            var result = new AsyncResult();
            var token = authStore.LoadToken();
            using (var client = new BearerHttpClient(token.AccessToken))
            {
                var data = new Dictionary<string, string>
                {
                    {"oldPassword", oldPassword},
                    {"newPassword", newPassword},
                };

                var uri = string.Format("/api/users/{0}/changePassword", login);

                using (var content = new FormUrlEncodedContent(data))
                using (var resp = await client.PostAsync(uri, content))
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        result.Succeed = true;
                        return result;
                    }

                    if (resp.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        result.ErrorMessage = "Произошла ошибка на сервере";
                    }
                    else if (resp.StatusCode == HttpStatusCode.BadRequest)
                    {
                        var err = await resp.Content.ReadAsStringAsync();
                        result.ErrorMessage = TranslateError(err);
                    }
                    return result;
                }
            }
        }
Пример #4
0
 public async Task<AsyncResult> UpdateUser(User user)
 {
     var result = new AsyncResult();
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var data = JsonConvert.SerializeObject(user);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/users", UriKind.Relative);
             using (var resp = await client.PutAsync(uri, content))
             {
                 if (resp.StatusCode == HttpStatusCode.OK)
                 {
                     result.Succeed = true;
                 }
             }
         }
     }
     return result;
 }