private string CalceSign(AuthorizationFilterContext context, string AppSecret) { string method = context.HttpContext.Request.Method.ToLower(); string path = context.HttpContext.Request.Path.Value; string thSign; if (method == "get" || method == "delete") { IQueryCollection reqUrls = context.HttpContext.Request.Query; StringBuilder sb = new StringBuilder(); var querys = reqUrls.OrderBy(k => k.Key).Select(e => e.Key + "=" + e.Value); string query = string.Join("&", querys); thSign = MD5Helper.CalcMD5(path + "?" + query + AppSecret); } else { //读取报文体 var requestBodyStream = new MemoryStream(); context.HttpContext.Request.Body.CopyTo(requestBodyStream); requestBodyStream.Seek(0, SeekOrigin.Begin); string body = new StreamReader(requestBodyStream, Encoding.UTF8).ReadToEnd(); requestBodyStream.Seek(0, SeekOrigin.Begin); context.HttpContext.Request.Body = requestBodyStream; thSign = MD5Helper.CalcMD5(path + AppSecret + body); } return(thSign); }
public async Task <long> AddNewAsync(AddAdminUserDTO dto) { AdminUserEntity entity = new AdminUserEntity(); entity.Age = dto.Age; entity.Gender = dto.Gender; entity.Name = dto.Name; entity.PhoneNum = dto.PhoneNum; entity.Salt = MD5Helper.GetSalt(10); entity.PasswordHash = MD5Helper.CalcMD5(dto.Password + entity.Salt); using (AdminUserContext ctx = new AdminUserContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); var phoneAdminUser = await bs.GetAll().SingleOrDefaultAsync(e => e.PhoneNum == dto.PhoneNum); if (phoneAdminUser != null) { throw new Exception("电弧号码已存在"); } await ctx.AdminUsers.AddAsync(entity); await ctx.SaveChangesAsync(); return(entity.Id); } }
public async Task <long> AddNewAsync(AddUserDTO dto) { UserEntity en = new UserEntity(); en.Email = dto.Email; en.Gender = dto.Gender; en.NickName = dto.NickName; en.Salt = MD5Helper.GetSalt(10); en.PasswordHash = MD5Helper.CalcMD5(dto.Password + en.Salt); using (UserContext ctx = new UserContext()) { BaseService <UserEntity> bs = new BaseService <UserEntity>(ctx); var emailEntity = await bs.GetAll().SingleOrDefaultAsync(e => e.Email == dto.Email || e.NickName == dto.NickName); if (emailEntity != null) { throw new Exception("用户邮箱或者昵称已存在"); } await ctx.Users.AddAsync(en); await ctx.SaveChangesAsync(); return(en.Id); } }
public async Task <SDKResult> RequestAsync <T>(HttpMethod method, string url, T data) where T : class { if (string.IsNullOrEmpty(url) || data == null) { throw new ArgumentException($"url或者queryStringData不能为空"); } using (HttpRequestMessage requestMessage = new HttpRequestMessage()) { requestMessage.Method = method; string sign; if (method == HttpMethod.Get || method == HttpMethod.Delete) { IDictionary <string, string> queryStringData; if (data is IDictionary <string, string> ) { queryStringData = (IDictionary <string, string>)data; } else { queryStringData = GetDict(data); } var queries = queryStringData.OrderBy(e => e.Key) .Select(e => $"{e.Key}={e.Value}"); string queryString = string.Join("&", queries); Uri uri = new Uri(ServerRoot + url + "?" + queryString); string path = new Regex("^/.+(/api/.*)$").Match(uri.PathAndQuery).Groups[1].Value; sign = MD5Helper.CalcMD5(path + Setting.AppSecret); requestMessage.Headers.Add("sign", sign); requestMessage.Headers.Add("appKey", Setting.AppKey); requestMessage.RequestUri = uri; return(await SendAsync(requestMessage)); } else if (method == HttpMethod.Put || method == HttpMethod.Post) { string json = JsonConvert.SerializeObject(data); using (StringContent content = new StringContent(json)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); requestMessage.Content = content; Uri uri = new Uri(ServerRoot + url); requestMessage.RequestUri = uri; string path = new Regex("^/.+(/api/.*)$").Match(uri.PathAndQuery).Groups[1].Value; sign = MD5Helper.CalcMD5(path + Setting.AppSecret + json); requestMessage.Headers.Add("sign", sign); requestMessage.Headers.Add("appKey", Setting.AppKey); return(await SendAsync(requestMessage)); } } else { throw new ApplicationException("未支持的方法"); } } }
public async Task EditorPasswordAsync(long id, string newPassword) { using (AdminUserContext ctx = new AdminUserContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); var entity = await bs.GetAll().SingleAsync(e => e.Id == id); entity.PasswordHash = MD5Helper.CalcMD5(newPassword + entity.Salt); await ctx.SaveChangesAsync(); } }
public async Task EditorPasswordAsync(long id, string newPassword) { using (UserContext ctx = new UserContext()) { BaseService <UserEntity> userBs = new BaseService <UserEntity>(ctx); var user = await userBs.GetAll().SingleAsync(e => e.Id == id); user.PasswordHash = MD5Helper.CalcMD5(newPassword + user.Salt); await ctx.SaveChangesAsync(); } }
public async Task <bool> CheckLoginAsync(string phoneNum, string password) { using (UserCenterDbContext ctx = new UserCenterDbContext()) { var user = await ctx.Users.SingleOrDefaultAsync(u => u.PhoneNum == phoneNum); if (user == null) { return(false); } string inputHash = MD5Helper.CalcMD5(password + user.PasswordSalt); return(user.PasswordHash == inputHash); } }
public async Task ChangePasswordAsync(long id, string newPassword) { using (AdminUserContext ctx = new AdminUserContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); AdminUserEntity entity = await bs.GetAll().SingleOrDefaultAsync(e => e.Id == id); if (entity == null) { throw new ArgumentException($"管理员{id}不存在"); } entity.PasswordHash = MD5Helper.CalcMD5(newPassword + entity.Salt); await ctx.SaveChangesAsync(); } }
public async Task <bool> LoginAsync(string email, string password) { using (UserContext ctx = new UserContext()) { BaseService <UserEntity> userBs = new BaseService <UserEntity>(ctx); var user = await userBs.GetAll().AsNoTracking().SingleOrDefaultAsync(e => e.Email == email); if (user == null) { return(false); } if (user.PasswordHash != MD5Helper.CalcMD5(password + user.Salt)) { return(false); } return(true); } }
public async Task <List <ListUserDTO> > GetByIdsAsync(List <long> userIds) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < userIds.Count; i++) { if (i == userIds.Count - 1) { sb.Append($"ids={userIds.ElementAt(i)}"); } else { sb.Append($"ids={userIds.ElementAt(i)}&"); } } string queryString = sb.ToString(); using (HttpRequestMessage requestMessage = new HttpRequestMessage()) { requestMessage.Method = HttpMethod.Get; Uri uri = new Uri(client.ServerRoot + "GetByIds?" + queryString); requestMessage.RequestUri = uri; string path = new Regex("^/.+(/api/.*)$").Match(uri.PathAndQuery).Groups[1].Value; string sign = MD5Helper.CalcMD5(path + client.Setting.AppSecret); requestMessage.Headers.Add("sign", sign); requestMessage.Headers.Add("appKey", client.Setting.AppKey); SDKResult result = await client.SendAsync(requestMessage); JObject obj = JsonConvert.DeserializeObject <JObject>(result.Result); if (result.StatusCode == System.Net.HttpStatusCode.BadRequest) { ErrorMsg = obj["errorMsg"].ToString(); return(null); } else if (result.StatusCode == System.Net.HttpStatusCode.OK) { return(JsonConvert.DeserializeObject <List <ListUserDTO> >(obj["data"] == null ? "" : obj["data"].ToString())); } else { throw new ApplicationException("未知的错误"); } } }
public async Task <bool> LoginAsync(string phoneNum, string password) { using (AdminUserContext ctx = new AdminUserContext()) { BaseService <AdminUserEntity> userBs = new BaseService <AdminUserEntity>(ctx); var user = await userBs.GetAll() .AsNoTracking() .SingleOrDefaultAsync(e => e.PhoneNum == phoneNum); if (user == null) { return(false); } if (!user.PasswordHash.Equals(MD5Helper.CalcMD5(password + user.Salt), StringComparison.InvariantCultureIgnoreCase)) { return(false); } return(true); } }
public async Task <long> AddNewAsync(string phoneNum, string nickName, string password) { using (UserCenterDbContext ctx = new UserCenterDbContext()) { if (await ctx.Users.AnyAsync(u => u.PhoneNum == phoneNum)) { throw new ApplicationException("手机号:" + phoneNum + "已经存在"); } UserEntity userEntity = new UserEntity(); userEntity.NickName = nickName; userEntity.PhoneNum = phoneNum; string salt = new Random().Next(10000, 99999).ToString(); string hash = MD5Helper.CalcMD5(password + salt); userEntity.PasswordSalt = salt; userEntity.PasswordHash = hash; ctx.Users.Add(userEntity); await ctx.SaveChangesAsync(); return(userEntity.Id); } }