示例#1
0
        public string Authtenticate(string user, string password)
        {
            string formatUser = user.ToLower().Trim();

            var userDB = db.Usuario.Where(x =>
                                          x.NickName.ToLower().Trim().Equals(formatUser) &&
                                          x.Password.Equals(password))
                         .Include(x => x.IdRolNavigation)
                         .Select(x => new UserDTO
            {
                Id      = x.IdUsuario,
                NomUser = x.NomUsuario,
                NomRol  = x.IdRolNavigation.NomRol
            })
                         .FirstOrDefault();

            if (userDB == null)
            {
                return("NotAuthorized");
            }

            string token = JWTTokenGenerator.GenereteJWTToken(userDB, Global.Key);

            return(token);
        }
示例#2
0
        //log in a user
        //this is most likely use for api call
        public async Task <UserViewModel> LogIn(LogInViewModel viewModel)
        {
            //check for user
            var user = await _userManager.FindByEmailAsync(viewModel.UserName);

            //user does not exist
            if (user == null)
            {
                throw new NullReferenceException("Invalid user name or password");
            }

            //user exist
            //validate username with password
            var result = await _userManager.CheckPasswordAsync(user, viewModel.Password);

            var tokenGenerator = new JWTTokenGenerator(this._config, user);

            //password match
            if (result)
            {
                return(new UserViewModel
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    UserName = user.Email,
                    IsUserVerify = user.EmailConfirmed,
                    Token = tokenGenerator.Token
                });
            }
            throw new InvalidOperationException("Invalid user name or password");
        }
示例#3
0
        public ActionResult Post([FromBody] AuthParameters login)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var identity = _authService.Auth(login);

            if (identity == null)
            {
                return(BadRequest("Invalid username or password."));
            }

            var token = JWTTokenGenerator.GenerateJSWToken(identity);

            return(Ok(new { Token = token }));
        }
示例#4
0
        private string CreateToken(HttpContext context)
        {
            var    user    = context.Request.ReadAsAsync <AppUser>().Result;
            var    isValid = new SignInManager(user.Username, user.Password).ValidateUser();
            string token   = null;

            if (isValid)
            {
                token = new JWTTokenGenerator(context).CreateToken(new AuthOptions
                {
                    Name     = "Abc",
                    UserName = user.Username,
                    Role     = "Admin"
                });
            }

            return(token);
        }