Beispiel #1
0
        /// <summary>
        /// Получить токен для входа.
        /// </summary>
        /// <param name="email">Адрес электронной почты</param>
        /// <param name="password">Пароль</param>
        /// <returns>Item1 - успех операции, Item2 - сообщение об ошибке или результат</returns>
        public Tuple <bool, String> LogIn(String email, String password)
        {
            var resultEmail = IsValidEmail(email);

            if (!resultEmail.Item1)
            {
                return(resultEmail);
            }
            var resultPassword = IsValidPassword(password);

            if (!resultPassword.Item1)
            {
                return(resultPassword);
            }
            try
            {
                using (chatContext = new ChatEntities())
                {
                    var user = chatContext.Users.FirstOrDefault(x => x.Email == email);
                    if (user == null || !PasswordCrypt.ComparePasswords(password, user.PasswordHash, user.PasswordSalt))
                    {
                        return(new Tuple <bool, string>(false, "Error! Login and/or password do not match"));
                    }
                    var token = GenerateToken(user);
                    chatContext.AccessTokens.Add(token);
                    int res = chatContext.SaveChanges();
                    if (res == 1)
                    {
                        return(new Tuple <bool, string>(true, token.Token));
                    }
                    else
                    {
                        return(new Tuple <bool, string>(false, "Error! Internal service Error"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new Tuple <bool, string>(false, ex.Message));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Регистрация пользователя
        /// </summary>
        /// <param name="email">Email</param>
        /// <param name="login">Login</param>
        /// <param name="password">Password</param>
        /// <returns>Item1 - успех операции, Item2 - сообщение об ошибке или результат</returns>
        public Tuple <bool, String> Registration(String email, String login, String password)
        {
            var resultEmail = IsValidEmail(email);

            if (!resultEmail.Item1)
            {
                return(resultEmail);
            }
            var resultPassword = IsValidPassword(password);

            if (!resultPassword.Item1)
            {
                return(resultPassword);
            }
            var resultLogin = IsValidLogin(login);

            if (!resultLogin.Item1)
            {
                return(resultLogin);
            }

            try
            {
                using (chatContext = new ChatEntities())
                {
                    if (chatContext.Users.Any(x => x.Email == email))
                    {
                        return(new Tuple <bool, string>(false, "User with this email is already exist"));
                    }
                    if (chatContext.Users.Any(x => x.Login == login))
                    {
                        return(new Tuple <bool, string>(false, "User with this login is already exist"));
                    }
                    var  passKeyAndSalt = PasswordCrypt.GetHashFromPassword(password);
                    User user           = new User()
                    {
                        PasswordHash = passKeyAndSalt.Item1,
                        PasswordSalt = passKeyAndSalt.Item2,
                        Login        = login,
                        Name         = login,
                        Email        = email
                    };
                    chatContext.Users.Add(user);
                    if (chatContext.SaveChanges() != 1)
                    {
                        return(new Tuple <bool, string>(false, "Error! Internal service Error"));
                    }

                    var token = GenerateToken(user);
                    chatContext.AccessTokens.Add(token);
                    int res = chatContext.SaveChanges();
                    if (res == 1)
                    {
                        return(new Tuple <bool, string>(true, token.Token));
                    }
                    else
                    {
                        return(new Tuple <bool, string>(false, "Error! Internal service Error"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new Tuple <bool, string>(false, ex.Message));
            }
        }