public object GetTencentCosFederationToken([FromServices] ITencentCosProvider tencentCosProvider) { string key = "HaoHaoPlay_Back_FederationToken"; GetFederationTokenResponse result; var tokenCache = RedisHelper.Get(key); if (tokenCache.IsNullOrWhiteSpace()) { result = tencentCosProvider.GetFederationToken(); RedisHelper.Set(key, JsonConvert.SerializeObject(result), 7200); } else { result = JsonConvert.DeserializeObject <GetFederationTokenResponse>(tokenCache); } return(new { result.Credentials, result.ExpiredTime, StartTime = H_Util.GetUnixTimestamp(DateTime.Now), result.RequestId, }); }
public object GetUploadAvatarInfo([FromServices] IConfiguration config, [FromServices] ICurrentUser currentUser) { return(new { Bucket = config["TencentCos:Bucket"], Key = config["TencentCos:AvatarKey"] + $"/{currentUser.Id}_{H_Util.GetUnixTimestamp()}", Region = config["TencentCos:Region"] }); }
/// <summary> /// 更新用户密码 /// </summary> /// <param name="userId"></param> /// <param name="oldPwd"></param> /// <param name="newPwd"></param> /// <returns></returns> public async Task UpdatePwd(long userId, string oldPwd, string newPwd) { var user = await Get(userId); oldPwd = H_EncryptProvider.HMACSHA256(oldPwd, _appSettings.Key.Sha256Key); H_AssertEx.That(user.Password != oldPwd, "原密码错误"); user.PasswordLevel = (PasswordLevel)H_Util.CheckPasswordLevel(newPwd); user.Password = H_EncryptProvider.HMACSHA256(newPwd, _appSettings.Key.Sha256Key); await _userRep.UpdateAsync(user, user => new { user.Password, user.PasswordLevel }); }
public async Task Add(UserAddInput input) { var user = input.Adapt <SysUser>(); user.FirstNameInitial = WordsHelper.GetFirstPinyin(user.Name.Substring(0, 1)); user.PasswordLevel = (PasswordLevel)H_Util.CheckPasswordLevel(user.Password); user.Password = H_EncryptProvider.HMACSHA256(user.Password, _appSettings.Key.Sha256Key); user.Enabled = true; var role = await _roleDomainService.Get(input.RoleId.Value); user.RoleId = role.Id; user.RoleName = role.Name; user.AuthNumbers = role.AuthNumbers; user.RoleLevel = role.Level; await _userDomainService.Add(user); }
/// <summary> /// 用户 /// </summary> private void MapUser(TypeAdapterConfig config) { config.ForType <SysUser, UserOutput>() .Map(x => x.GenderString, a => a.Gender.ToDescription()) .Map(x => x.EnabledString, a => a.Enabled.IsTrue() ? "有效" : "注销") .Map(x => x.Age, a => DateTime.Now.Year - a.Birthday.Value.Year); config.ForType <SysUser, UserDetailOutput>() .Map(x => x.GenderString, a => a.Gender.ToDescription()) .Map(x => x.EnabledString, a => a.Enabled.IsTrue() ? "有效" : "注销"); config.ForType <SysUser, UserSecurityOutput>() .Map(x => x.PasswordLevel, a => a.PasswordLevel.ToDescription()) .Map(x => x.Phone, a => H_Util.HidePhoneNumber(a.Phone)) .Map(x => x.Email, a => H_Util.HideEmailNumber(a.Email)); config.ForType <UserQueryInput, UserQuery>() .Map(x => x.OrderByConditions, a => a.SortFields.ToOrderByConditions(a.SortTypes)); }
/// <summary> /// 生成Jwt /// </summary> /// <param name="timeNow"></param> /// <param name="expireTime"></param> /// <param name="jti"></param> /// <param name="user"></param> /// <returns></returns> private string CreateJwt(DateTime timeNow, DateTime expireTime, Guid jti, SysUser user) { var claims = new Claim[] { new Claim(JwtRegisteredClaimNames.Sub, _appSettings.Jwt.Subject), //主题 new Claim(JwtRegisteredClaimNames.Jti, jti.ToString()), //针对当前 token 的唯一标识 jwt的唯一身份标识,避免重复 new Claim(JwtRegisteredClaimNames.Sid, user.Id.ToString()), new Claim(JwtRegisteredClaimNames.Iat, H_Util.GetUnixTimestamp(timeNow).ToString(), ClaimValueTypes.Integer64), //token 创建时间 }; var secretKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_appSettings.Jwt.SecretKey)); var jwt = new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken( issuer: _appSettings.Jwt.Issuer, audience: _appSettings.Jwt.Audience, claims: claims, notBefore: timeNow, //生效时间 expires: expireTime, //过期时间 signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256) )); return(jwt); }