Esempio n. 1
0
        private void Reigstration_Click(object sender, RoutedEventArgs e)
        {
            var stateContainer = StateContainer.GetStateContainer();

            stateContainer.registrationStateService.ChangeState();

            if (!stateContainer.registrationStateService.IsLogin)
            {
                HaveAccount.Visibility = Visibility.Hidden;
                EmailBox.Visibility    = Visibility.Visible;
                Registration.Content   = "Login";
                EnterType.Text         = "Registration";
                LoginButton.Content    = "Registration";
            }
            else
            {
                HaveAccount.Visibility = Visibility.Visible;
                EmailBox.Visibility    = Visibility.Hidden;
                Registration.Content   = "Registration";
                EnterType.Text         = "Login";
                LoginButton.Content    = "Login";
            }

            ChangeTypeSign();
        }
        public async Task SetSessionAsync(string acessToken)
        {
            var authRequest = new AuthRequest(acessToken);
            var session     = await userProvider.GetSessionAsync(UserId);

            var aes = new AesCrypt();

            var stateContainer = StateContainer.GetStateContainer();

            string cryptedSession = aes.Crypt(stateContainer.sessionStateService.StrongKey, session.SessionId);

            await authRequest.MakeRequestAsync(ConfigurationManager.AppSettings.Get("devUrl") + Urls.SetServerSessionUrl + cryptedSession, HttpMethod.Get, null);
        }
Esempio n. 3
0
        private async void LoginButton_Click_1(object sender, RoutedEventArgs e)
        {
            var stateContainer = StateContainer.GetStateContainer();

            string errorMessage = "";

            if (stateContainer.registrationStateService.IsLogin)
            {
                errorMessage = ValidateLogin(LoginBox.Text, PasswordBox.Password, EmailBox.Text, false);
            }
            else
            {
                errorMessage = ValidateLogin(LoginBox.Text, PasswordBox.Password, EmailBox.Text, true);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                LoginMessageBlock.Text       = errorMessage;
                LoginMessageBlock.Visibility = Visibility.Visible;

                return;
            }

            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("userName", LoginBox.Text);
            parameters.Add("password", PasswordBox.Password);

            if (stateContainer.registrationStateService.IsLogin)
            {
                var users = await userProvider.GetAllUsersAsync();

                var currentUser = users.FirstOrDefault(x => x.Login == LoginBox.Text && x.Password == HashService.GetHash(PasswordBox.Password));

                if (currentUser != null)
                {
                    var strongKey = await userProvider.GetStrongKeyAsync(currentUser.Id);

                    var session = await userProvider.GetSessionAsync(currentUser.Id);

                    tokenService = new TokenService(currentUser.Id);

                    string acessToken = await tokenService.MakeAuthTokenAsync(currentUser.Id);

                    stateContainer.sessionStateService.SetStateAsync(strongKey: strongKey.Key, clientPrivateKey: session.ClientPrivateKey,
                                                                     clientPublicKey: session.ClientPublicKey, serverPublicKey: session.ServerPublicKey,
                                                                     acessToken: acessToken);

                    ContactsForm contactForm = new ContactsForm();
                    contactForm.Show();
                    Close();
                }
                else
                {
                    var httpRequestMessage = new HttpRequestMessage();
                    httpRequestMessage.Method     = HttpMethod.Post;
                    httpRequestMessage.RequestUri = new Uri(ConfigurationManager.AppSettings.Get("devUrl") + Urls.AuthUrl);

                    var form = new MultipartFormDataContent();

                    foreach (var parameter in parameters)
                    {
                        form.Add(new StringContent(parameter.Value), parameter.Key);
                    }

                    httpRequestMessage.Content = form;

                    var authResponse = await baseHttpRequest.httpClient.SendAsync(httpRequestMessage);

                    switch (authResponse.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                        LoginMessageBlock.Text       = "Wrong login or password!";
                        LoginMessageBlock.Visibility = Visibility.Visible;
                        break;

                    case HttpStatusCode.Unauthorized:

                        LoginMessageBlock.Text       = "Wrong login or password!";
                        LoginMessageBlock.Visibility = Visibility.Visible;

                        break;

                    case HttpStatusCode.InternalServerError:
                        LoginMessageBlock.Text       = "Internal server error!";
                        LoginMessageBlock.Visibility = Visibility.Visible;
                        break;

                    case HttpStatusCode.OK:

                        string content = await authResponse.Content.ReadAsStringAsync();

                        var authResult = JsonConvert.DeserializeObject <AuthResult>(content);

                        await userProvider.СreateOrUpdateAuthStorageAsync(authResult.UserId, authResult.AccessToken, authResult.RefreshToken);

                        var sessionService = new SessionService(authResult.UserId);
                        await sessionService.MakeSessionAsync(authResult.AccessToken, authResult.RefreshToken);

                        await userProvider.CreateUserAsync(authResult.UserId, LoginBox.Text, HashService.GetHash(PasswordBox.Password));

                        ContactsForm contactForm = new ContactsForm();
                        contactForm.Show();
                        Close();

                        break;
                    }
                }
            }
            else
            {
                var httpRequest = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Post;
                httpRequest.RequestUri = new Uri(ConfigurationManager.AppSettings.Get("devUrl") + Urls.RegisterUrl);

                var form = new MultipartFormDataContent();

                foreach (var parameter in parameters)
                {
                    form.Add(new StringContent(parameter.Value), parameter.Key);
                }

                httpRequest.Content = form;

                var registerResult = await baseHttpRequest.httpClient.SendAsync(httpRequest);


                switch (registerResult.StatusCode)
                {
                case HttpStatusCode.OK:

                    string testerToken = ConfigurationManager.AppSettings.Get("testerToken");

                    var authUserInfo = JsonConvert.DeserializeObject <AuthResult>(
                        await registerResult.Content.ReadAsStringAsync()
                        );

                    var phoneConfirmRequestMessage = new HttpRequestMessage();
                    phoneConfirmRequestMessage.Method     = HttpMethod.Get;
                    phoneConfirmRequestMessage.RequestUri = new Uri(ConfigurationManager.AppSettings.Get("devUrl") + $"{Urls.ConfirmPhoneTestUrl}/{authUserInfo.UserId}");
                    phoneConfirmRequestMessage.Headers.Add("TesterToken", testerToken);

                    var phoneConfirmResult = await baseHttpRequest.httpClient.SendAsync(phoneConfirmRequestMessage);

                    if (phoneConfirmResult.StatusCode == HttpStatusCode.OK)
                    {
                        var sessionService = new SessionService(authUserInfo.UserId);

                        await userProvider.СreateOrUpdateAuthStorageAsync(authUserInfo.UserId, authUserInfo.AccessToken, authUserInfo.RefreshToken);

                        await sessionService.MakeSessionAsync(authUserInfo.AccessToken, authUserInfo.RefreshToken);

                        await userProvider.CreateUserAsync(authUserInfo.UserId, LoginBox.Text, HashService.GetHash(PasswordBox.Password));

                        ContactsForm contact = new ContactsForm();
                        contact.Show();
                        Close();
                    }
                    else
                    {
                        MessageBox.Show($"Something was error");
                    }

                    break;

                case HttpStatusCode.BadRequest:
                    break;

                case HttpStatusCode.InternalServerError:
                    break;
                }
            }

            //LoginMessageBlock.Text = "Wrong login or password!";
            //LoginMessageBlock.Visibility = Visibility.Visible;
        }
        public ContactsForm()
        {
            stateContainer = StateContainer.GetStateContainer();

            tokenService = new TokenService(stateContainer.sessionStateService.UserId);

            string token = AsyncHelper.RunSync(() => tokenService.MakeAuthTokenAsync(stateContainer.sessionStateService.UserId));

            connection = new HubConnectionBuilder()
                         .WithUrl(ConfigurationManager.AppSettings.Get("devUrl") + "messanger", options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(token);
            })
                         .WithAutomaticReconnect()
                         .Build();

            InitializeComponent();
            contacts = new ObservableCollection <TestContacts>
            {
                new TestContacts()
                {
                    Name     = "Pendos", LastMessage = "Sosi",
                    Messages = new ObservableCollection <TestMessage>
                    {
                        new TestMessage()
                        {
                            Sender = "Pendos", Text = "Sosi"
                        },
                        new TestMessage()
                        {
                            Sender = Iam, Text = "Sam Sosi"
                        },
                        new TestMessage()
                        {
                            Sender = "Pendos", Text = "Okey("
                        }
                    }
                },
                new TestContacts()
                {
                    Name     = "Chera", LastMessage = "Ilya sdelai chto to",
                    Messages = new ObservableCollection <TestMessage>
                    {
                        new TestMessage()
                        {
                            Sender = "Chera", Text = "Sosi xyi"
                        },
                        new TestMessage()
                        {
                            Sender = Iam, Text = "Sam Sosi"
                        },
                        new TestMessage()
                        {
                            Sender = "Chera", Text = "Okey("
                        }
                    }
                },
                new TestContacts()
                {
                    Name = "Peder", LastMessage = "Ya daun"
                },
            };
            ContactsList.ItemsSource = contacts;
        }