Exemplo n.º 1
0
        public async Task <IActionResult> Login(LoginRequestModel model)
        {
            try
            {
                if (model != null)
                {
                    AccountLoginResponce Data = await account.AdminLogin(model);

                    if (Data.Token != null)
                    {
                        return(Ok(new { Success = true, Message = "Admin Login successful!!", Data }));
                    }
                    else
                    {
                        return(BadRequest(new { Success = false, Message = "Invalid credentials" }));
                    }
                }
                else
                {
                    return(BadRequest(new { Success = false, Message = "Invalid credentials" }));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(new { Success = false, Message = e.Message }));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Admins the login.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public async Task <AccountLoginResponce> AdminLogin(LoginRequestModel model)
        {
            try
            {
                AccountLoginResponce loginResponce = await notesRL.AdminLogin(model);

                if (loginResponce.Token != null)
                {
                    RedisEndpoint redisEndpoint = new RedisEndpoint("localhost", 6379);
                    using (RedisClient client = new RedisClient(redisEndpoint))
                    {
                        if (client.Get <string>(model.Email + model.Password) == null)
                        {
                            client.Set <string>(model.Email + model.Password, DateTime.Now.ToString());
                            loginResponce.LoginTime = client.Get <string>(model.Email + model.Password);
                        }
                        else
                        {
                            client.Remove(model.Email + model.Password);
                            client.Set <string>(model.Email + model.Password, DateTime.Now.ToString());
                            loginResponce.LoginTime = client.Get <string>(model.Email + model.Password);
                        }
                    }
                }
                return(loginResponce);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 3
0
 public IActionResult Login(LoginRequestModel model)
 {
     try
     {
         if (model != null)
         {
             AccountLoginResponce Data = account.Login(model);
             if (Data.Token != null)
             {
                 return(Ok(new { Success = true, Message = "Login successful!!", Data }));
             }
             else
             {
                 return(BadRequest(new { Success = false, Message = "Wrong Email or Password" }));
             }
         }
         else
         {
             return(BadRequest(new { Success = false, Message = "Invalid credentials" }));
         }
     }
     catch (Exception e)
     {
         return(BadRequest(new { Success = false, Message = e.Message }));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Admins the login.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public async Task <AccountLoginResponce> AdminLogin(LoginRequestModel model)
        {
            try
            {
                SqlConnection connection = DBConnection();
                string        encrypted  = EncryptPassword(model.Password);
                SqlCommand    command    = StoreProcedureConnection("spAdminLogin", connection);
                command.Parameters.AddWithValue("@Email", model.Email);
                command.Parameters.AddWithValue("@Password", encrypted);
                connection.Open();
                SqlDataReader dataReader = await command.ExecuteReaderAsync();

                AccountLoginResponce loginResponce = new AccountLoginResponce();
                while (dataReader.Read())
                {
                    if ((dataReader["Email"].ToString()).Equals(model.Email))
                    {
                        loginResponce.Id          = (int)dataReader["Id"];
                        loginResponce.FirstName   = dataReader["FirstName"].ToString();
                        loginResponce.LastName    = dataReader["LastName"].ToString();
                        loginResponce.Email       = dataReader["Email"].ToString();
                        loginResponce.PhoneNumber = dataReader["PhoneNumber"].ToString();
                        loginResponce.UserAddress = dataReader["UserAddress"].ToString();
                        loginResponce.ServiceType = dataReader["ServiceType"].ToString();
                        loginResponce.UserType    = dataReader["UserType"].ToString();
                        loginResponce.Token       = GenrateJWTToken(model.Email, loginResponce.Id);
                        break;
                    }
                }
                connection.Close();
                return(loginResponce);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 5
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Login([FromBody] LoginData model)
        {
            try
            {
                if (model != null)
                {
                    Regex regexEmail = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Regex regexPhone = new Regex(@"^\+\d{12}");
                    Match matchEmail = (model.userLogInfo != null) ? regexEmail.Match(model.userLogInfo) : regexEmail.Match("");
                    Match matchPhone = (model.userLogInfo != null) ? regexPhone.Match(model.userLogInfo) : regexPhone.Match("");


                    if (matchEmail.Success)
                    {
                        BookSender.Data.Models.User user = await _context.Users
                                                           .Include(u => u.Role)
                                                           .FirstOrDefaultAsync(u => u.Email == model.userLogInfo && u.Password == model.Password);


                        AccountLoginResponce acc = new AccountLoginResponce
                        {
                            Login   = user.Email,
                            Name    = user.FirstName,
                            Surname = user.LastName,
                            Role    = user.Role != null ? user.Role.Name : "Guest",
                        };

                        await Authenticate(user);

                        return(Json(acc));
                    }
                    else if (matchPhone.Success)
                    {
                        BookSender.Data.Models.User user = await _context.Users
                                                           .Include(u => u.Role)
                                                           .FirstOrDefaultAsync(u => u.PhoneNumber == model.userLogInfo && u.Password == model.Password);

                        AccountLoginResponce acc = new AccountLoginResponce
                        {
                            Login   = user.Email,
                            Name    = user.FirstName,
                            Surname = user.LastName,
                            Role    = user.Role != null ? user.Role.Name : "Guest",
                        };

                        await Authenticate(user, isEmailAuth : false);

                        return(Json(acc));
                    }
                    else
                    {
                        return(Json("'Answer': 'Wrong user credetials'"));
                    }
                }
                else
                {
                    return(Json(new LoginData()));
                }
            }
            catch (Exception ex)
            {
                return(Json($" 'Answer' : 'Error = {ex.Message}' "));
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> LoginWithFacebook([FromBody] FaceBookLoginModel model)
        {
            try
            {
                if (model.email != null)
                {
                    string DefaultPassWord = SmtpClientLibrary.SendKey(model.email, "*****@*****.**", "Zaebalo45809");
                    string DefaultPhone    = "9379992";

                    Regex regexEmail = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Match matchEmail = (model.email != null) ? regexEmail.Match(model.email) : regexEmail.Match("");


                    if (matchEmail.Success)
                    {
                        AccountLoginResponce acc = new AccountLoginResponce();

                        User user = await _context.Users
                                    .Include(u => u.Role)
                                    .FirstOrDefaultAsync(u => u.Email == model.email);

                        if (user == null)
                        {
                            string[] names = model.name.Split(" ");

                            _context.Users.Add(new User
                            {
                                Password       = DefaultPassWord,
                                PhoneNumber    = DefaultPhone,
                                Email          = model.email,
                                FirstName      = names[0],
                                LastName       = names[1],
                                RatingStatusId = 1,
                                RoleId         = 2
                            });

                            var role = await _context.Roles.FirstOrDefaultAsync(r => r.Id == 2);

                            await _context.SaveChangesAsync();

                            acc.Login   = user.Email;
                            acc.Name    = names[0];
                            acc.Surname = names[1];
                            acc.Role    = role.Name;
                        }
                        else
                        {
                            acc.Login   = user.Email;
                            acc.Name    = user.FirstName;
                            acc.Surname = user.LastName;
                            acc.Role    = user.Role != null ? user.Role.Name : "Guest";
                        }

                        await Authenticate(user);

                        return(Json(acc));
                    }
                    else
                    {
                        return(Json($" 'Answer' : 'Error = BadEmail' "));
                    }
                }
                else
                {
                    return(Json(new AccountLoginResponce()));
                }
            }
            catch (Exception ex)
            {
                return(Json($" 'Answer' : 'Error = {ex.Message}' "));
            }
        }