public void DeleteUser(string userId) { if(String.IsNullOrWhiteSpace(userId)) { return; } TokenRepository tr = new TokenRepository(); tr.DeleteTokens(userId); UserRepository ur = new UserRepository(); ur.DeleteUser(userId); }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { Stopwatch watch = new Stopwatch(); watch.Start(); HttpMessage message = new HttpMessage(request); string requestData = request.Content.ReadAsStringAsync().Result; StringBuilder requestContent = new StringBuilder(); #region log try { if (Log.IsInfoEnabled) { string api = message.GetRequestUriTemplate(); requestContent.Append("\r\n\tRequest ClientIP : ").Append(message.GetClientIP()).Append("\r\n"); requestContent.Append("\tRequest URL : ").Append(message.GetHttpMethod()).Append(" ").Append(message.GetRequestUri()).Append("\r\n"); requestContent.Append("\tRequest Hearder : "); requestContent.Append(message.GetHttpHeader()); if (!String.IsNullOrEmpty(requestData)) { requestContent.Append("\r\n\tRequest Body : ").Append(requestData); } } } catch (Exception exception) { Log.Error(exception.ToString()); } #endregion SetContext(request); // Token validation if (Context.RequestUriTemplate == "apis/v1/token") { return base.SendAsync(request, cancellationToken); } if (String.IsNullOrEmpty(Context.Token)) { return HttpResponseHelper.SendAsync(UnAuthenticationResponse()); } else { TokenRepository tr = new TokenRepository(); var tokenModel = tr.GetTokenModel(Context.Token); if (tokenModel == null) { return HttpResponseHelper.SendAsync(UnAuthenticationResponse()); } else { Context.LoginName = tokenModel.UserName; var response = base.SendAsync(request, cancellationToken); #region Response Log if (Log.IsInfoEnabled) { try { Task<string> responseMessage = null; if (response.Result != null && response.Result.Content != null) { responseMessage = response.Result.Content.ReadAsStringAsync(); } watch.Stop(); int requestExcuteTime = Convert.ToInt32(watch.ElapsedMilliseconds); watch = null; requestContent.Append("\r\n\tResponse : "); if (responseMessage != null) { requestContent.Append(responseMessage.Result); } requestContent.Append("\r\n\tCost(ms) : ").Append(requestExcuteTime); Log.Info(requestContent.ToString()); } catch (Exception exception) { Log.Error(exception.ToString()); } } #endregion return response; } } }
public bool Login(string userName, string password,out UserModel user, bool isAdminLoginFromPortal = false,string clientIP = null,string macAddr = null) { user = null; bool isLogin = false; string md5Password = isAdminLoginFromPortal ? MD5Helper.GetMd5Hash(password) : password; UserRepository ur = new UserRepository(); User dbuser = ur.Login(userName, md5Password); if (dbuser != null) { user = ConvertDBUserToBizUser(dbuser); // 客户端用户不能登录后台管理系统 if (isAdminLoginFromPortal && (int)RoleType.ClientUsers == user.UserType) { isLogin = false; } else { isLogin = true; TokenRepository tr = new TokenRepository(); var tokenModel = new token { IP = clientIP, MacAddr = macAddr, UserName = userName }; user.Token = tr.NewToken(tokenModel); // login success, add login record LoginRecordRepository recordRep = new LoginRecordRepository(); LoginRecord loginRecord = new LoginRecord { IsLoginFromClient = isAdminLoginFromPortal ? 0 : 1, LoginIP = clientIP, UserName = userName }; recordRep.Add(loginRecord); } } return isLogin; }