Beispiel #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            _logger.Info("User submitted registration Form! Params: " + model.ToJson());

            if (ModelState.IsValid)
            {
                try
                {
                    var user = new ClientUsers {
                        UserName = model.UserName.Trim(' '), Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        _logger.Info("User registered Successfully!");

                        //Send notification to office mail
                        await EmailNotifications.RegiteredUserNotification(user.UserName, user.Email, user.PhoneNumber);

                        //Subscribe him if checked
                        if (model.IsSubscribed)
                        {
                            await MailListManager.Subscribe(model.Email);
                        }

                        var currentUser = await UserManager.FindByNameAsync(user.UserName);

                        await UserManager.AddToRoleAsync(currentUser.Id, "Client");

                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        if (Request.Url != null)
                        {
                            var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code },
                                                         Request.Url.Scheme);
                            await EmailNotifications.NoReplyMailService.SendHtmlEmailAsync(user.Email,
                                                                                           "Потвърдете акаунтът си в sProperties",
                                                                                           "За да потвърдите регистрацията си натиснете <a href=\"" + callbackUrl + "\">ТУК</a>");
                        }

                        return(RedirectToAction("Login", "Account", new { confirmation = "email" }));
                    }
                    AddErrorsToModelState(result);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Something went wrong during the registration!");
                }
            }

            _logger.Error("User registration form Invalid! Errors: " + ModelState.ToJson());

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public async Task <ActionResult> CreateOwner(OwnerCreateViewModel model)
        {
            _logger.Info("Creating Owner Account! Params: " + model.ToJson());

            if (!ModelState.IsValid)
            {
                _logger.Error("Creating Owner Account Form Invalid! Errors: " + ModelState.ToJson());
                return(Json(ModelState.ToDictionary()));
            }

            var owner = new OwnerUsers
            {
                UserName             = model.UserName,
                FirstName            = model.FirstName,
                LastName             = model.LastName,
                PhoneNumber          = model.PhoneNumber,
                Email                = model.Email,
                PhoneNumberConfirmed = true,
                BirthDate            = model.BirthDate
            };

            var result = await UserManager.CreateAsync(owner, model.Password);

            if (result.Succeeded)
            {
                _logger.Info("Owner Created Successfully!");
                //Send notification to office mail
                await EmailNotifications.RegiteredUserNotification(owner.UserName, owner.Email, owner.PhoneNumber);

                await UserManager.AddToRoleAsync(owner.Id, "Owner");

                return(Json(new UsersIdInfoViewModel
                {
                    Id = owner.Id,
                    Info = "Име: " + owner.FirstName + " " + (owner.LastName ?? "") + ", Телефон: " + owner.PhoneNumber
                }));
            }
            else
            {
                AddErrorsToModelState(result);
                _logger.Info("Owner Creation Failed! Errors: " + ModelState.ToJson());

                return(Json(ModelState.ToDictionary()));
            }
        }
Beispiel #3
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                _logger.Info("User is authenticated and will be redirected to Manage/Index");
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ClientUsers {
                    UserName = model.UserName, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.Info("User registered successfully with external login!");
                    //Send notification to office mail
                    await EmailNotifications.RegiteredUserNotification(user.UserName, user.Email, user.PhoneNumber);

                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrorsToModelState(result);
            }

            _logger.Error("Problem in external login Form! Errors: " + ModelState.ToJson());

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Beispiel #4
0
        public async Task <ActionResult> RegisterOwner(RegisterOwnerViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Checking the register code in the db and delete the code if exists
                if (OwnerRegisterCodeManager.CheckOwnerRegisterCode(model.RegisterCode))
                {
                    var owner = new OwnerUsers {
                        UserName = model.UserName.Trim(' '), Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(owner, model.Password);

                    if (result.Succeeded)
                    {
                        //Send notification to office mail
                        await EmailNotifications.RegiteredUserNotification(owner.UserName, owner.Email, owner.PhoneNumber);

                        var currentUser = await UserManager.FindByNameAsync(owner.UserName);

                        await UserManager.AddToRoleAsync(currentUser.Id, "Owner");

                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(owner.Id);

                        if (Request.Url != null)
                        {
                            var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = owner.Id, code }, Request.Url.Scheme);
                            await EmailNotifications.NoReplyMailService.SendHtmlEmailAsync(owner.Email, "Потвърдете акаунтът си в sProperties", "За да потвърдите регистрацията си натиснете <a href=\"" + callbackUrl + "\">ТУК</a>");
                        }

                        return(RedirectToAction("Login", "Account", new { confirm = "email" }));
                    }
                    AddErrorsToModelState(result);
                }
                else
                {
                    ModelState.AddModelError("RegisterCode", "Invalid Register Code");
                    return(View());
                }
            }

            return(View());
        }