private async void Register(object sender, RoutedEventArgs e)
        {
            UserViewModel user = new UserViewModel
            {
                Password = Password.Password,
                RepeatPassword = RepeatPassword.Password,
                Username = Username.Text
            };

            Backend.RegistrationResponse response = await Backend.Azure.Register(user);

            if (response.ValidationErrors != null && response.ValidationErrors.Any())
            {
                Error.Text = response.ValidationErrors.First().ErrorMessage;
            }
            else
            {
                if (response.BackendResponse == Backend.BackendResponse.Ok)
                {
                    Error.Text = "";
                    Frame.Navigate(typeof (LoginPage), user.Username);
                } else if (response.BackendResponse == Backend.BackendResponse.BadRequest)
                {
                    Error.Text = "Email is already used, please pick another one or log in";
                }
                else
                {
                    Error.Text = "Looks like something went wrong, try again later";
                }
            }
        }
Example #2
0
        public async Task<RegistrationResponse> Register(UserViewModel user)
        {
            List<ValidationResult> results = new List<ValidationResult>();
            if (!Validator.TryValidateObject(user, new ValidationContext(user), results, true))
            {
                return new RegistrationResponse(results);
            }
            else
            {
                if (user.Password != user.RepeatPassword)
                {
                    results.Add(new ValidationResult("Passwords are not the same"));
                    return new RegistrationResponse(results);
                }

                HttpWebRequest request = PopulateRequest(new Uri("/api/Account/Register", UriKind.Relative), POST);
                Task<Stream> outStream = request.GetRequestStreamAsync();
                using (StreamWriter writer = new StreamWriter(await outStream))
                {
                    string userData = JsonConvert.SerializeObject(user);
                    await writer.WriteAsync(userData);
                }

                try
                {
                    using (StreamReader response = new StreamReader((await request.GetResponseAsync()).GetResponseStream()))
                    {
                        return new RegistrationResponse(response: BackendResponse.Ok);
                    }
                }
                catch (WebException e)
                {
                    return new RegistrationResponse(response: BackendResponse.BadRequest);
                }
            }
        }