Beispiel #1
0
        public async Task <ActionResult> Register(RegistrationViewModel registrationModel)
        {
            var model = new LoginViewModel(new SigninViewModel(registrationModel.ReturnUrl), registrationModel);

            if (!ModelState.IsValid)
            {
                return(View("Login", model));
            }

            if (await _clientAccountsRepository.IsTraderWithEmailExistsAsync(registrationModel.Email))
            {
                ModelState.AddModelError("", $"Email {registrationModel.Email} is already in use.");
                return(View("Login", model));
            }

            string userIp    = this.GetIp();
            string referer   = null;
            string userAgent = this.GetUserAgent();

            if (!string.IsNullOrEmpty(registrationModel.Referer))
            {
                referer = new Uri(registrationModel.Referer).Host;
            }

            RegistrationResponse result = await _registrationClient.RegisterAsync(new RegistrationModel
            {
                Email     = registrationModel.Email,
                Password  = PasswordKeepingUtils.GetClientHashedPwd(registrationModel.RegistrationPassword),
                Ip        = userIp,
                Changer   = RecordChanger.Client,
                UserAgent = userAgent,
                Referer   = referer
            });

            if (result == null)
            {
                ModelState.AddModelError("", "Technical problems during registration.");
                return(View("Login", model));
            }

            var clientAccount = new Core.Clients.ClientAccount
            {
                Id              = result.Account.Id,
                Email           = result.Account.Email,
                Registered      = result.Account.Registered,
                NotificationsId = result.Account.NotificationsId,
                Phone           = result.Account.Phone
            };

            foreach (var registrationConsumer in _registrationConsumers)
            {
                registrationConsumer.ConsumeRegistration(clientAccount, userIp, CultureInfo.CurrentCulture.Name);
            }

            var identity = await _userManager.CreateUserIdentityAsync(clientAccount.Id, clientAccount.Email, registrationModel.Email);

            await HttpContext.Authentication.SignInAsync("ServerCookie", new ClaimsPrincipal(identity), new AuthenticationProperties());

            return(RedirectToAction("PersonalInformation", "Profile", new { returnUrl = registrationModel.ReturnUrl }));
        }
Beispiel #2
0
        public async Task <ActionResult> Signin(SigninViewModel loginModel)
        {
            var model = new LoginViewModel(loginModel, new RegistrationViewModel(loginModel.ReturnUrl));

            if (!ModelState.IsValid)
            {
                return(View("Login", model));
            }

            var clientAccount =
                await _clientAccountsRepository.AuthenticateAsync(loginModel.Username, loginModel.Password) ??
                //ToDo: to remove when migrated to hashes
                await
                _clientAccountsRepository.AuthenticateAsync(loginModel.Username,
                                                            PasswordKeepingUtils.GetClientHashedPwd(loginModel.Password));

            if (clientAccount == null)
            {
                ModelState.AddModelError("Username", " ");
                ModelState.AddModelError("Password", "Invalid user");
                return(View("Login", model));
            }

            var identity = await _userManager.CreateUserIdentityAsync(clientAccount, loginModel.Username);

            await
            HttpContext.Authentication.SignInAsync("ServerCookie", new ClaimsPrincipal(identity),
                                                   new AuthenticationProperties());

            return(RedirectToLocal(loginModel.ReturnUrl));
        }
        public static ClientAccountEntity CreateNew(IClientAccount clientAccount, string password)
        {
            var result = new ClientAccountEntity
            {
                PartitionKey    = GeneratePartitionKey(),
                RowKey          = Guid.NewGuid().ToString(),
                NotificationsId = Guid.NewGuid().ToString("N"),
                Email           = clientAccount.Email.ToLower(),
                Phone           = clientAccount.Phone,
                Registered      = clientAccount.Registered,
                PartnerId       = clientAccount.PartnerId
            };

            PasswordKeepingUtils.SetPassword((IPasswordKeeping)result, password);

            return(result);
        }
        public async Task <RegistrationResultModel> CompleteRegistration([FromBody] SignUpViewModel model)
        {
            var regResult = new RegistrationResultModel();

            if (ModelState.IsValid)
            {
                if (!model.Email.IsValidEmailAndRowKey())
                {
                    regResult.Errors.Add("Invalid email address");
                    return(regResult);
                }

                string userIp    = HttpContext.GetIp();
                string referer   = null;
                string userAgent = HttpContext.GetUserAgent();

                if (!string.IsNullOrEmpty(model.Referer))
                {
                    try
                    {
                        referer = new Uri(model.Referer).Host;
                    }
                    catch
                    {
                        regResult.Errors.Add("Invalid referer url");
                        return(regResult);
                    }
                }

                var code = await _verificationCodesRepository.GetCodeAsync(model.Key);

                RegistrationResponse result = await _registrationClient.RegisterAsync(new RegistrationModel
                {
                    Email     = model.Email,
                    Password  = PasswordKeepingUtils.GetClientHashedPwd(model.Password),
                    Ip        = userIp,
                    Changer   = RecordChanger.Client,
                    UserAgent = userAgent,
                    Referer   = referer,
                    CreatedAt = DateTime.UtcNow,
                    Cid       = code?.Cid,
                    Traffic   = code?.Traffic
                });

                regResult.RegistrationResponse = result;

                if (regResult.RegistrationResponse == null)
                {
                    regResult.Errors.Add("Technical problems during registration.");
                    return(regResult);
                }

                var identity = await _userManager.CreateUserIdentityAsync(result.Account.Id, result.Account.Email, model.Email, true);

                await HttpContext.SignInAsync(OpenIdConnectConstantsExt.Auth.DefaultScheme, new ClaimsPrincipal(identity));

                await _profileActionHandler.UpdatePersonalInformation(result.Account.Id, model.FirstName, model.LastName);

                await _verificationCodesRepository.DeleteCodesAsync(model.Email);
            }
            else
            {
                var errors = ModelState.Values
                             .Where(item => item.ValidationState == ModelValidationState.Invalid)
                             .SelectMany(item => item.Errors);

                foreach (var error in errors)
                {
                    regResult.Errors.Add(error.ErrorMessage);
                }
            }

            return(regResult);
        }