Ejemplo n.º 1
0
        public async Task <ActionResult> Login(AuthVm model)
        {
            var response = await AccountFunctions.Login(model.Email, model.Password, model.IsRemember);

            if (response.Code == (int)EnumList.Response.Success)
            {
                Notification.Success = response.Message;

                if (Url.IsLocalUrl(model.ReturnUrl))
                {
                    return(Redirect(model.ReturnUrl));
                }
                return(RedirectToAction("Index", "Dashboard"));
            }

            if (response.Message.Equals("Lockout", StringComparison.InvariantCultureIgnoreCase))
            {
                return(View("Lockout", new LockoutVm {
                    Email = model.Email, UnlockDate = response.LockOutDateTime
                }));
            }

            if (response.Message.Equals("You need to confirm your email.", StringComparison.InvariantCultureIgnoreCase))
            {
                StaticValues.NotifyActionRequiredMsg =
                    "Your email address is not verified. Please, <a href=# class=\"alert-link\">click here</a>, to verify your account.";
            }
            else
            {
                StaticValues.NotifyError = response.Message;
            }

            return(View("Index", model));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Login([FromBody] AuthVm loginVm)
        {
            var response = await _userService.AuthenticateUser(loginVm);

            if (response.Item1)
            {
                return(Json(new { status = response.Item1, message = response.Item2, data = response.Item3 }));
                //return RedirectToAction("profile", "accounts");
            }
            return(Json(new { status = response.Item1, message = response.Item2, data = response.Item3 }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] AuthVm vm)
        {
            var identity = await this.GetClaimsIdentity(vm.UserName, vm.Password);

            if (identity == null)
            {
                return(this.Unauthorized());
            }

            var jwt = await JwtGenerator.GenerateJwt(identity, this.jwtFactory, vm.UserName, this.jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

            return(new OkObjectResult(jwt));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Index(string returnUrl, string userId, string code)
        {
            var model = new AuthVm {
                ReturnUrl = returnUrl
            };

            if (string.IsNullOrWhiteSpace(userId) || string.IsNullOrWhiteSpace(code))
            {
                return(View(model));
            }
            await AccountFunctions.ConfirmEmail(userId, code);

            return(View(model));
        }
Ejemplo n.º 5
0
        public bool Authenticate(UnitOfWork uow, AuthVm authvm)
        {
            try
            {
                if (uow.UserRepository.IsExist(authvm.Username, authvm.Password))
                {
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                return(false);

                throw;
            }
        }
Ejemplo n.º 6
0
        public ApiResponse Post([FromBody] AuthVm authVm)
        {
            try
            {
                if (authBl.Authenticate(_uow, authVm))
                {
                    return(new ApiResponse()
                    {
                        Data = authService.GenerateToken(), Message = "Success", Status = 200
                    });
                }
                return(new ApiResponse()
                {
                    Data = null, Message = "unauthorized", Status = 200
                });
            }
            catch (System.Exception ex)
            {
                return(ApiResponse.Exception());

                throw;
            }
        }
Ejemplo n.º 7
0
        public async Task <Tuple <bool, string, AuthResponse> > AuthenticateUser(AuthVm login)
        {
            try
            {
                if (string.IsNullOrEmpty(login.Phone_Email) || string.IsNullOrEmpty(login.Password))
                {
                    return(new Tuple <bool, string, AuthResponse>(false, "Fill in all the fields", null));
                }
                User user = new User();
                if (!login.Phone_Email.Contains("@"))
                {
                    user = await _userManager.Users.FirstOrDefaultAsync(x => x.PhoneNumber == login.Phone_Email);
                }
                else
                {
                    user = await _userManager.FindByEmailAsync(login.Phone_Email);
                }

                if (user == null)
                {
                    return(new Tuple <bool, string, AuthResponse>(false, "Your email/phone number and or password is incorrect", null));
                }

                var userHasPassword = await _userManager.HasPasswordAsync(user);

                if (!userHasPassword)
                {
                    return(new Tuple <bool, string, AuthResponse>(false, "sorry you do not have a valid password", null));
                }

                var userHasValidPassword = await _userManager.CheckPasswordAsync(user, login.Password);

                if (!userHasValidPassword)
                {
                    return(new Tuple <bool, string, AuthResponse>(false, "Your password is invalid", null));
                }



                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.NameIdentifier, user.Id),
                    new Claim("Surname", user.Surname),
                    new Claim("OtherNames", user.OtherNames),
                    new Claim(ClaimTypes.MobilePhone, user.PhoneNumber),
                };

                var          token    = JwtTokenGenerator.GenerateAccessToken(claims, _configuration).ToString();
                AuthResponse response = new AuthResponse
                {
                    Surname     = user.Surname,
                    OtherNames  = user.OtherNames,
                    Token       = token,
                    PhoneNumber = user.PhoneNumber,
                    ImagePath   = user.ImagePath
                };

                var result = await _signinManager.PasswordSignInAsync(login.Phone_Email, login.Password, false, false);



                return(new Tuple <bool, string, AuthResponse>(result.Succeeded, " ", response));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }