Ejemplo n.º 1
0
        public async Task <IActionResult> Confirm(long?id) // View Confirm page
        {
            if (id == null)
            {
                return(NotFound());
            }

            UserApp userApp = null;

            try
            {
                await Task.Run(() =>
                {
                    userApp = new UserUtils().GetUserById(_context, (long)id);
                });
            }
            catch (Exception)
            {
                HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                return(RedirectToAction("RegisterError", "Account"));
            }


            if (userApp != null)
            {
                Cryptographer cryptographer = new Cryptographer().Create(userApp.Upassword);

                string decodeEmail = cryptographer.Decode(userApp.Email);


                EmailResponce goodEmail = await new EmailService().ChekEmaileService(decodeEmail);

                switch (goodEmail.Success)
                {
                case 1:

                    EmailService emailService = new EmailService();

                    var callbackUrl = Url.Action(
                        "ConfirmEmail",
                        "Account",
                        new { Token = userApp.Id, Email = userApp.Email },
                        protocol: HttpContext.Request.Scheme);


                    await emailService.SendEmailAsync(decodeEmail, "Confirm email", $"Подтвердите регистрацию, перейдя по ссылке: <a href='{callbackUrl}'>Confirm email</a>");


                    ViewBag.Name = goodEmail.EmailData.Item1;
                    ViewBag.Href = goodEmail.EmailData.Item2;

                    return(View());

                case -1:
                    HttpContext.Session.SetString(ERROR, "Mail is not correct.");
                    return(RedirectToAction("RegisterError", "Account"));
                }
            }

            return(NotFound());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Register([Bind("Name,LastName,Email,Password,Confirm")] RegisterForm registerForm, string date_register) // Register
        {
            HttpContext.Session.Remove(ERROR);

            if (ModelState.IsValid)
            {
                string passwordHash = new HashConvertor().GetHash(registerForm.Password.Trim().ToLower());
                string emailHash    = new HashConvertor().GetHash(registerForm.Email.Trim().ToLower());

                SomeData someData = null;

                try
                {
                    someData = await _context.SomeDatas.LastOrDefaultAsync(sd => sd.Data1 == emailHash);
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (someData != null)
                {
                    return(RedirectToAction("Signin", "Account"));
                }

                Cryptographer cryptographer = new Cryptographer().Create(passwordHash);

                string emailEncode = cryptographer.Encode(registerForm.Email.Trim().ToLower());

                UserApp userApp = null;

                try
                {
                    await Task.Run(() =>
                    {
                        userApp = new UserUtils().GetUser(_context, emailEncode, passwordHash);
                    });
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("RegisterError", "Account"));
                }

                if (userApp != null)
                {
                    return(RedirectToAction("Signin", "Account"));
                }
                else
                {
                    EmailResponce goodEmail = await new EmailService().ChekEmaileService(registerForm.Email.Trim().ToLower());

                    switch (goodEmail.Success)
                    {
                    case 1:

                        string name     = cryptographer.Encode(registerForm.Name.Trim().ToLower());
                        string lastname = cryptographer.Encode(registerForm.LastName.Trim().ToLower());
                        string email    = emailEncode;
                        string password = passwordHash;



                        double localDate = Convert.ToDouble(date_register);
                        registerForm.Time = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(localDate);


                        try
                        {
                            _context.Add(new SomeData {
                                Data1 = emailHash,
                                Data2 = passwordHash
                            });
                            await _context.SaveChangesAsync();

                            await Task.Run(() =>
                            {
                                userApp = new UserUtils().RegisterNewUser(_context, name, lastname, email, password, registerForm.Time);
                            });
                        }
                        catch (Exception)
                        {
                            HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                            return(RedirectToAction("RegisterError", "Account"));
                        }

                        return(RedirectToAction("Confirm", "Account", new { id = userApp.Id }));

                    case -1:
                        HttpContext.Session.SetString(ERROR, "Mail is not correct.");
                        return(RedirectToAction("RegisterError", "Account"));
                    }
                }
            }
            return(View());
        }