Esempio n. 1
0
        public async Task <JsonResult> Login(string mobile, string password)
        {
            //验证
            var member = new MemberDto {
                Id = 1, Name = "测试"
            };
            //生成Token
            var tokenVersion = DateTime.Now.Ticks.ToString();

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Version, tokenVersion)
                , new Claim(ClaimTypes.NameIdentifier, member.Id.ToString())
                , new Claim(ClaimTypes.Name, member.Name)
                , new Claim(ClaimTypes.Role, ((int)MemberRole.Default).ToString())
            };

            var token = _jwtProvider.CreateToken(claims);

            var loginMember = new LoginMember {
                Id   = member.Id,
                Name = member.Name
            };

            //如果当前已登陆,则退出当前登录
            DoLogout();

            _cacheService.Add(string.Format(CacheKeyDefinition.ApiMemberToken, member.Id, tokenVersion), loginMember, TimeSpan.FromMinutes(_jwtConfig.ExpiredMinutes));

            return(Json(token));
        }
Esempio n. 2
0
        public async Task <JsonResult> Login(string mobile, string password)
        {
            //取得用户信息
            var user = await _service.MobileLoginAsync(mobile, password);

            //生成Token
            var tokenVersion = DateTime.Now.Ticks.ToString();

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Version, tokenVersion)
                , new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
                , new Claim(ClaimTypes.Name, user.NickName)
                , new Claim(ClaimTypes.Role, ((int)UserRole.Default).ToString())
            };

            var token = _jwtProvider.CreateToken(claims);

            var loginMember = new LoginMember {
                Id   = user.Id,
                Name = user.NickName
            };

            //如果当前已登陆,则退出当前登录
            await DoLogoutAsync();

            await _cacheService.SetAsync(string.Format(CacheKeyDefinition.ApiUserToken, user.Id, tokenVersion), loginMember, TimeSpan.FromMinutes(_jwtConfig.ExpiredMinutes));

            return(Json(token));
        }
Esempio n. 3
0
        public async Task <JwtView> Login(LoginAccountView view)
        {
            User    user  = _mapper.Map <LoginAccountView, User>(view);
            JwtView token = _jwtProvider.CreateToken(user);

            return(token);
        }
Esempio n. 4
0
        public async Task <JsonWebToken> HandleAsync(SignIn query)
        {
            var dbUser = await _dbContext.Users
                         .FirstOrDefaultAsync(user => user.Email.Equals(query.Email));

            if (dbUser == null || !_hasher.IsValidAsync(dbUser, query.Password))
            {
                throw new ServiceException("Invalid email or password");
            }

            return(_jwtProvider.CreateToken(dbUser.Id));
        }
Esempio n. 5
0
        public async Task <JwtDto> HandleAsync(GetToken query)
        {
            var user = await _usersRepository.GetAsync(query.Email);

            if (user is null)
            {
                throw new Exception("Invalid credentials.");
            }

            var isPasswordValid = _passwordService.IsValid(user.Password, query.Password);

            if (!isPasswordValid)
            {
                throw new Exception("Invalid credentials.");
            }

            return(_jwtProvider.CreateToken(user.Id.ToString("N"), user.Role));
        }
Esempio n. 6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                throw new KuArgNullException();
            }

            //图片验证码
#if !DEBUG
            if (Input.ImageCode.IsNullOrEmpty())
            {
                throw new KuException("请输入验证码!");
            }
#endif
            if (!Input.ImageCode.IsNullOrEmpty())
            {
                var code = HttpContext.Session.GetString($"ImageValidateCode_login");
                HttpContext.Session.Remove("ImageValidateCode_login");
                if (!Input.ImageCode.EqualOrdinalIgnoreCase(code))
                {
                    throw new KuException(1, "验证码出错!");
                }
            }
            var user = await _userService.LoginAsync(Input.Account, Input.Password);

            if (user == null)
            {
                throw new KuException("登陆出错!");
            }
            //UserActionLogDto log = new UserActionLogDto();
            //log.Operation = "用户登陆";
            //log.ControllerName = "Home";
            //log.ActionName = "Login";
            //log.UserId = user.Id;
            //log.Ip = HttpContext.IpAddress();
            //log.Url = HttpContext.RequestPath();
            //log.UrlReferrer = HttpContext.UrlReferrer();
            //log.UserAgent = HttpContext.UserAgent().Substr(0, 250);
            //log.Method = HttpContext.Request.Method;
            //log.QueryString = HttpContext.Request.QueryString.ToString().Substr(0, 250);
            //log.CreateTime = DateTime.Now;

            //await _eventPublisher.PublishAsync(log);

            var claims = new List <Claim>()
            {
                new Claim("Account", user.Account)
                , new Claim(ClaimTypes.Name, user.NickName)
                , new Claim("HeadImage", user.HeadImage ?? "")
                , new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
            };

            var token = _jwtProvider.CreateToken(claims);

            base.Response.Cookies.Append(_jwtAuthConfig.CookieName, token, new CookieOptions
            {
                HttpOnly = true
            });

            //清除用户权限缓存
            await _cacheService.RemoveAsync(string.Format(CacheKeyDefinition.UserAuthCode, user.Id));

            await _cacheService.RemoveAsync(string.Format(CacheKeyDefinition.UserAuthCodeEncrypt, user.Id));

            //Cookie中保存用户信息
            base.Response.Cookies.Append("user.name", user.NickName);
            base.Response.Cookies.Append("user.headimage", user.HeadImage ?? "");

            return(JsonData(true));
        }