/// <summary> /// 登录,根据用户名,手机,邮箱来登录,当同一手机,邮箱不止一个用户使用时,将登录不成功 /// </summary> /// <param name="request"></param> /// <param name="validate">验证密码</param> /// <returns></returns> public async Task <LoginResult> Login(LoginRequest request, bool validate = true) { LoginResult result = new LoginResult(); string requestPass = null; //登录时回传的记住我的信息,从记住我的信息恢复用户的登录用户名密码 if (request.Remember?.Length > 10) { RestoreFormRemember(request); requestPass = request.Password; } AppUser user = null; request.Account = request.Account.ToLower(); if (user == null && CommOp.IsEmail(request.Account)) { user = await _user.GetByEmailAsync(request.Account); } if (user == null && CommOp.IsPhoneNumber(request.Account)) { user = await _user.GetByTelAsync(request.Account); } if (user == null) { user = await _user.GetAsync(request.Account); } if (user == null) { return(MSG.UserNotExists); } if (requestPass == null) { requestPass = Encryption.MD5(request.Password + user.PasswordSalt); } if (user.Password != requestPass && validate) { return(MSG.PasswordError); } UserSession session = new UserSession { Token = CommOp.NewId(), Account = user.Account, Id = user.Id, LastOpTime = DateTime.Now }; var cacheKey = GetCacheKey(session.Token); var sessionBytes = Encoding.UTF8.GetBytes(session.ToJson(false)); await this.DistributedCache.SetAsync(cacheKey, sessionBytes, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(this.Expires) }); this.MemoryCache.Set(cacheKey, session); DataGateService ds = Consts.Get <DataGateService>(); //写最后登录时间,并进一步判断用户是否存在 await ds.UpdateAsync("UpdateLastLoginTime", new { id = user.Id, LastLoginDate = session.LastOpTime }); //要求“记住我”时,将登录信息加密回传,根据服务端的加密 if (request.Remember == "1") { request.Remember = Encryption.Encrypt(String.Join("|", user.Account, user.Password)); } return(new LoginResult { ExpireIn = Expires, Token = session.Token, Remember = request.Remember }); }
/// <summary> /// 登录,根据用户名,手机,邮箱来登录,当同一手机,邮箱不止一个用户使用时,将不成登录成功 /// </summary> /// <param name="request"></param> /// <param name="validate">验证密码</param> /// <returns></returns> public async Task <LoginResult> Login(LoginRequest request, bool validate = true) { LoginResult result = new LoginResult(); string requestPass = null; //登录时回传的记住我的信息,从记住我的信息恢复用户的登录用户名密码 if (request.Remember?.Length > 10) { RestoreFormRemember(request); requestPass = request.Password; } AppUser user = null; if (user == null && CommOp.IsEmail(request.Account)) { user = await _user.GetByEmailAsync(request.Account); } if (user == null && CommOp.IsPhoneNumber(request.Account)) { user = await _user.GetByTelAsync(request.Account); } if (user == null) { user = await _user.GetAsync(request.Account); } if (user == null) { return(MSG.UserNotExists); } if (requestPass == null) { requestPass = Encryption.MD5(request.Password + user.PasswordSalt); } if (user.Password != requestPass && validate) { return(MSG.PasswordError); } UserSession session = new UserSession { Token = CommOp.NewId(), Account = user.Account, Id = user.Id, LastOpTime = DateTime.Now }; _sessionDict.TryAdd(session.Token, session); DataGateService ds = Consts.Get <DataGateService>(); await ds.UpdateAsync("UpdateLastLoginTime", new { id = user.Id, LastLoginDate = session.LastOpTime }); //要求“记住我”时,将登录信息加密回传,根据服务端的加密 if (request.Remember == "1") { request.Remember = Encryption.Encrypt(String.Join("|", user.Account, user.Password)); } return(new LoginResult { ExpireIn = Expires, Token = session.Token, Remember = request.Remember }); }