예제 #1
0
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User us = await db.Users.Where(u => u.Mail == model.Mail).FirstOrDefaultAsync();

                if (us != null)
                {
                    return(Ok(new { msg = $"Пользователь с {model.Mail} уже зарегистрирован" }));
                }
                us = await db.Users.Where(u => u.Login == model.Login).FirstOrDefaultAsync();

                if (us != null)
                {
                    return(Ok(new { msg = $"Пользователь с {model.Login} уже зарегистрирован" }));
                }
                User user = new User {
                    Mail   = model.Mail, Login = model.Login, Password = HashClass.GetHash(model.Password),
                    RoleId = 1, Avatar = "user_icon.png"
                };
                try
                {
                    db.Users.Add(user);
                    await db.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.InnerException.Message));
                }
                return(Ok(new { msg = "" }));
            }
            return(BadRequest());
        }
예제 #2
0
        public async Task <IActionResult> Register(RegisterModel model, string baseUrl)
        {
            User us = await db.Users.Where(u => u.Mail == model.Mail).FirstOrDefaultAsync();

            if (us != null)
            {
                return(new OkObjectResult(new { msg = $"Пользователь с {model.Mail} уже зарегистрирован" }));
            }
            us = await db.Users.Where(u => u.Login == model.Login).FirstOrDefaultAsync();

            if (us != null)
            {
                return(new OkObjectResult(new { msg = $"Пользователь с {model.Login} уже зарегистрирован" }));
            }
            User user = new User
            {
                Mail             = model.Mail,
                Login            = model.Login,
                Password         = HashClass.GetHash(model.Password),
                RoleId           = 1,
                IsMailConfirmed  = false,
                VerifyCode       = Guid.NewGuid(),
                Avatar           = options.Value.DefaultUserImageLink,
                AvatarFile       = options.Value.DefaultUserImageFile,
                RegistrationDate = DateTime.Now
            };

            try
            {
                db.Users.Add(user);
                await db.SaveChangesAsync();

                var emailInfo = new EmailInfo();
                emailInfo.Subject = "Подтверждение почты в приложении MusicApp";
                emailInfo.Body    = $"<div><p>Кликните по ссылке ниже, чтобы подтвердить свою почту</p><a href='{baseUrl}{confirmMailLink}?userId={user.UserId}&verifyCode={user.VerifyCode}'>Подтвердить почту</a></div>";
                emailInfo.ToMails.Add(user.Mail);
                var emailResult = emailManager.Send(emailInfo);
                if (!emailResult.Sended)
                {
                    throw new Exception(emailResult.ErrorMessage);
                }
                return(new OkResult());
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.InnerException.Message));
            }
        }
예제 #3
0
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User us = await db.Users.Where(u => u.Mail == model.Mail).FirstOrDefaultAsync();

                if (us != null)
                {
                    return(Ok(new { msg = $"Пользователь с {model.Mail} уже зарегистрирован" }));
                }
                us = await db.Users.Where(u => u.Login == model.Login).FirstOrDefaultAsync();

                if (us != null)
                {
                    return(Ok(new { msg = $"Пользователь с {model.Login} уже зарегистрирован" }));
                }
                User user = new User {
                    Mail   = model.Mail, Login = model.Login, Password = HashClass.GetHash(model.Password),
                    RoleId = 1, IsMailConfirmed = false
                };
                try
                {
                    db.Users.Add(user);
                    await db.SaveChangesAsync();

                    MailClass mailClass = new MailClass();
                    mailClass.Subject = "Подтверждение почты";
                    mailClass.Body    = mailService.GetMailBody(authOptions.Value.Issuer + $"api/RegisterUser/ConfirmEmail?username={model.Login}", model.Login);
                    mailClass.ToMails = new List <string>()
                    {
                        model.Mail
                    };
                    await mailService.SendMail(mailClass);

                    return(Ok(new { msg = $"Регистрация прошла успешно, на {model.Mail} было отправлено письмо для подтверждения почты" }));
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.InnerException.Message));
                }
            }
            return(BadRequest());
        }
예제 #4
0
        public async Task <IActionResult> ForgotPasswordChange(ForgotPasswordModel model)
        {
            try
            {
                var user = await db.Users.FindAsync(model.UserId);

                if (user == null)
                {
                    return(new NotFoundObjectResult(new { msg = "Пользователя с указанным id не существует" }));
                }
                if (user.VerifyCode != model.VerifyCode)
                {
                    return(new NotFoundObjectResult(new { msg = "Неверный код верификации" }));
                }
                user.Password = HashClass.GetHash(model.Password);
                await db.SaveChangesAsync();

                return(new OkResult());
            }
            catch
            {
                return(new StatusCodeResult(500));
            }
        }
예제 #5
0
        public async Task <IActionResult> ChangeUserPassword(ChangePasswordModel model, int userId)
        {
            try
            {
                var user = await db.Users.FindAsync(userId);

                if (user == null)
                {
                    return(new NotFoundResult());
                }
                if (user.Password != HashClass.GetHash(model.OldPassword))
                {
                    return(new ForbidResult());
                }
                user.Password = HashClass.GetHash(model.NewPassword);
                await db.SaveChangesAsync();

                return(new OkResult());
            }
            catch
            {
                return(new StatusCodeResult(500));
            }
        }
예제 #6
0
 public async Task <User> GetUserByNameEmail(AuthModel model)
 {
     return(await db.Users.Where(u => (u.Login == model.Login || u.Mail == model.Login) && u.Password == HashClass.GetHash(model.Password)).FirstOrDefaultAsync());
 }
예제 #7
0
        public async Task <IActionResult> Login([FromBody] AuthModel model)
        {
            User user = await db.Users.Where(u => u.Mail == model.Email && u.Password == HashClass.GetHash(model.Password)).FirstOrDefaultAsync();

            if (user != null)
            {
                //Generate token
                var token = GenerateJWT(user);
                return(Ok(new { access_token = token }));
            }
            return(Unauthorized());
        }