public async Task <IResponseOutput> Login(AuthLoginInput input) { var sw = new Stopwatch(); sw.Start(); var res = (await _authService.LoginAsync(input)) as IResponseOutput; sw.Stop(); #region 添加登录日志 var loginLogAddInput = new LoginLogAddInput() { CreatedUserName = input.UserName, ElapsedMilliseconds = sw.ElapsedMilliseconds, Status = res.Success, Msg = res.Msg }; AuthLoginOutput user = null; if (res.Success) { user = (res as IResponseOutput <AuthLoginOutput>).Data; loginLogAddInput.CreatedUserId = user.Id; loginLogAddInput.RealName = user.Name; } await _loginLogService.AddAsync(loginLogAddInput); #endregion if (!res.Success) { return(res); } #region 生成token信息 var token = _userToken.Build(new[] { new Claim(ClaimAttributes.UserId, user.Id.ToString()), new Claim(ClaimAttributes.UserName, user.UserName), new Claim(ClaimAttributes.UserRealName, user.Name) }); #endregion return(ResponseOutput.Ok(new { token })); }
/// <summary> /// 获得token /// </summary> /// <param name="output"></param> /// <returns></returns> private IResponseOutput GetToken(ResponseOutput<AuthLoginOutput> output) { if (!output.Success) { return ResponseOutput.NotOk(output.Msg); } var user = output.Data; var token = _userToken.Build(new[] { new Claim(ClaimAttributes.UserId, user.Id.ToString()), new Claim(ClaimAttributes.UserName, user.UserName), new Claim(ClaimAttributes.UserNickName, user.NickName) }); return ResponseOutput.Ok(new { token }); }
public async Task <IResponseOutput> LoginAsync(AuthLoginInput input) { #region 验证码校验 var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey); var exists = await _cache.ExistsAsync(verifyCodeKey); if (exists) { var verifyCode = await _cache.GetAsync(verifyCodeKey); if (string.IsNullOrEmpty(verifyCode)) { return(ResponseOutput.NotOk("验证码已过期!", 1)); } if (verifyCode.ToLower() != input.VerifyCode.ToLower()) { return(ResponseOutput.NotOk("验证码输入有误!", 2)); } await _cache.DelAsync(verifyCodeKey); } else { return(ResponseOutput.NotOk("验证码已过期!", 1)); } #endregion var user = (await _userRepository.Select.Where(a => a.UserName == input.UserName).ToOneAsync()); if (!(user?.Id > 0)) { return(ResponseOutput.NotOk("账号输入有误!", 3)); } #region 解密 if (input.PasswordKey.NotNull()) { var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, input.PasswordKey); var existsPasswordKey = await _cache.ExistsAsync(passwordEncryptKey); if (existsPasswordKey) { var secretKey = await _cache.GetAsync(passwordEncryptKey); if (passwordEncryptKey.IsNull()) { return(ResponseOutput.NotOk("解密失败!", 1)); } input.Password = DesEncrypt.Decrypt(input.Password, secretKey); await _cache.DelAsync(passwordEncryptKey); } else { return(ResponseOutput.NotOk("解密失败!", 1)); } } #endregion var password = MD5Encrypt.Encrypt32(input.Password); if (user.Password != password) { return(ResponseOutput.NotOk("密码输入有误!", 4)); } //生成token信息 var claims = new[] { new Claim(ClaimAttributes.UserId, user.Id.ToString()), new Claim(ClaimAttributes.UserName, user.UserName), new Claim(ClaimAttributes.UserRealName, user.Name) }; var token = _userToken.Build(claims); return(ResponseOutput.Ok(new { token })); }