Ejemplo n.º 1
0
        public static async Task <Response> IsHavePower(IsPowerIng ResultMsg, int Id)
        {
            Response response = new Response
            {
                code = Convert.ToInt32(StatusEnum.Failed)
            };

            response.msg = "暂无权限";
            try
            {
                var list = await SqlDapperHelper.ReturnListTAsync <Role>("select SR.Id,SR.AdminId,SR.PowerSetId,SR.CreateTime,SP.ActionName,SP.ControllerNmae,SP.PowerName,SP.MenuId  from Sys_Role SR join Sys_PowerSet SP  on SR.PowerSetId=SP.Id  where SR.AdminId=@AdminId", new { AdminId = Id });

                var isHave = list.Any(x => x.ControllerNmae == ResultMsg.ControllerName && x.ActionName == ResultMsg.ActionName);
                if (isHave)
                {
                    response.code = Convert.ToInt32(StatusEnum.Succeed);
                    response.msg  = "正常";
                }
                else
                {
                    response.code = Convert.ToInt32(StatusEnum.Error);
                    response.msg  = "暂无权限";
                }
            }
            catch (Exception ex)
            {
                LogHelp.Error(ex);
            }
            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public IActionResult LoginOut( )
        {
            var result = new Response <Login>()
            {
                code = Convert.ToInt32(StatusEnum.Failed)
            };

            result.url = "/Home/Index";
            result.msg = "退出失败!";
            try
            {
                //检查用户状态

                //加密存入缓存
                //// CacheHelper.SetAbsolute("token",token,15*60);
                //HttpContext.Response.Cookies.Delete("tokens");

                HttpContext.Response.Cookies.Delete("tokens");
                result.code = Convert.ToInt32(StatusEnum.Succeed);
                result.url  = "/Home/Index";
                result.msg  = "退出成功!";
                //是否需要返回登录成功后的实体
                return(Ok(result));
            }
            catch (Exception ex)
            {
                result.code = Convert.ToInt32(StatusEnum.Error);
                result.msg  = "内部请求出错";//内部请求出错
                LogHelp.Error(ex, ex.Message);
            }
            return(Ok(result));
        }
Ejemplo n.º 3
0
 public ActionResult Index()
 {
     LogHelp.Debug("Debug");
     LogHelp.Info("Info");
     LogHelp.Error("Error");
     return(View());
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 接收MQ消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="func"></param>
        /// <param name="queueName"></param>
        public static void GetMQ <T>(Func <T, bool> func, string queueName)
        {
            using (var channel = ConnectionMQ.Connection().CreateModel())
            {
                //事件基本消费者
                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

                //接收到消息事件
                consumer.Received += (ch, ea) =>
                {
                    var message = Encoding.UTF8.GetString(ea.Body);
                    try
                    {
                        var item = JsonConvert.DeserializeObject <T>(message);
                        func(item);
                    }
                    catch (Exception ex)
                    {
                        LogHelp.Error(ex);
                    }
                    //确认该消息已被消费
                    channel.BasicAck(ea.DeliveryTag, false);
                };
                //启动消费者 设置为手动应答消息
                channel.BasicConsume(queueName, false, consumer);
            }
        }
Ejemplo n.º 5
0
        public IActionResult LoginAct(string userName, string passWord)
        {
            var result = new Response()
            {
                code = Convert.ToInt32(StatusEnum.Failed)
            };

            try
            {
                if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(passWord))
                {
                    result.msg = "用户名或密码错误!";
                    return(Ok(result));
                }

                //验证码,后期需要则加上

                //密码加密
                passWord = Convert.ToBase64String(BaseController.AESEncrypt(JsonConvert.SerializeObject(passWord)));
                result   = LoginService.LoginAct(userName, passWord);
            }
            catch (Exception ex)
            {
                result.code = Convert.ToInt32(StatusEnum.Error);
                result.msg  = "内部请求出错";//内部请求出错
                LogHelp.Error(ex, ex.Message);
            }
            return(Ok(result));
        }
Ejemplo n.º 6
0
        public JsonResult Login(Login login)
        {
            Response <object> result = new Response <object>()
            {
                code = Convert.ToInt32(Status.Failed)
            };
            var count = SqlDapperHelper.ReturnT <int>("select count(1) from [User] where Phone=@Phone and Password=@Password", login);

            if (count > 0)
            {
                result.code = Convert.ToInt32(Status.Succeed);
                result.msg  = "登录成功";
                Login tokens = SqlDapperHelper.ReturnT <Login>("select * from [User] where Phone=@Phone and Password=@Password", login);

                try
                {
                    //存redis
                    RedisManager.redisHelp.SetValue("Login", JsonConvert.SerializeObject(tokens));
                }
                catch (Exception ex)
                {
                    LogHelp.Error(ex);
                }


                try
                {
                    //登录加密保存信息

                    //var token = AESEncrypt(JsonConvert.SerializeObject(tokens));
                    // Convert.ToBase64String(Encode(Encoding.UTF8.GetBytes(password)));

                    var token = Convert.ToBase64String(AESEncrypt(JsonConvert.SerializeObject(tokens)));
                    //SignIn(tokens, true).Wait();

                    //登录成功,信息加入MQ

                    PushMQ.SendMQ(tokens, Key.PushMQUserKey);
                }
                catch (Exception ex)
                {
                    LogHelp.Error(ex);
                }
                return(Json(new { result }));
            }

            //测试日志
            LogHelp.Debug("登录失败:账号" + login.Phone + "  密码:" + login.Password);
            result.msg = "登录失败";
            return(Json(new { result }));
        }
Ejemplo n.º 7
0
        private static Task HandleExceptionAsync(HttpContext context, Exception ex)
        {
            var code = HttpStatusCode.InternalServerError; // 500 if unexpected

            //if (ex is NotFound) code = HttpStatusCode.NotFound;
            //else if (ex is UnauthorizedAccessException) code = HttpStatusCode.Unauthorized;
            //else if (ex is MyException) code = HttpStatusCode.BadRequest;
            if (ex is UnauthorizedAccessException)
            {
                code = HttpStatusCode.Unauthorized;
            }
            LogHelp.Error(ex);
            var result = JsonConvert.SerializeObject(new { error = ex.Message });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            return(context.Response.WriteAsync(result));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 查询  单个值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="indexDb"></param>
 /// <returns></returns>
 public T GetT <T>(string key, int indexDb = 0)
 {
     try
     {
         var        db     = redis.GetDatabase(indexDb);
         RedisValue result = db.StringGet(key);
         if (string.IsNullOrWhiteSpace(result))
         {
             return(default(T));
         }
         return(JsonConvert.DeserializeObject <T>(result));
     }
     catch (Exception ex)
     {
         LogHelp.Error(ex);
         return(default(T));
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 获取服务器信息
        /// </summary>
        /// <param name="serverId"></param>
        /// <returns></returns>
        public static Dictionary <string, Dictionary <string, string> > GetRedisInfo(string serverId)
        {
            Dictionary <string, Dictionary <string, string> > infoResponse = null;

            try
            {
                RedisServerModel model = RedisServerConfig.RedisServers.FirstOrDefault(p => p.ServerId == serverId);
                if (model != null)
                {
                    var server = GetRedisServer(model.ServerHost);
                    infoResponse = server.Info().ToDictionary(p => p.Key, p => p.ToDictionary(x => x.Key, x => x.Value));
                }
            }
            catch (Exception ex)
            {
                LogHelp.Error("GetRedisInfo方法异常", ex);
            }
            return(infoResponse);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 获取客户端信息
        /// </summary>
        /// <param name="serverId"></param>
        /// <returns></returns>
        public static List <ClientInfoModel> GetClients(string serverId)
        {
            List <ClientInfoModel> clients = new List <ClientInfoModel>();

            try
            {
                RedisServerModel model = RedisServerConfig.RedisServers.FirstOrDefault(p => p.ServerId == serverId);
                if (model != null)
                {
                    var server = GetRedisServer(model.ServerHost);
                    clients = server.ClientList().Select(ConvertClientInfoModel).OrderBy(p => p.Host).ThenBy(p => p.Port).ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelp.Error("GetClients方法异常", ex);
            }
            return(clients);
        }
Ejemplo n.º 11
0
        public static async Task <R> Post <T, R>(string host, string url, T entity)
        {
            var apiRect = default(R);

            try
            {
                HttpClient          clientAuth = new HttpClient();
                string              requestUrl = string.Format("{0}{1}", host, url);
                HttpResponseMessage response   = await clientAuth.PostAsJsonAsync(requestUrl, entity);

                response.EnsureSuccessStatusCode();
                string rect = await response.Content.ReadAsStringAsync();

                apiRect = JsonConvert.DeserializeObject <R>(rect);
            }
            catch (Exception ex)
            {
                LogHelp.Error(ex);
            }
            return(apiRect);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// @ming 登录操作
        /// </summary>
        /// <param name="loginInfo">登录实体</param>
        /// <returns></returns>
        public IActionResult LoginAct([FromForm] Login loginInfo)
        {
            var result = new Response <Login>()
            {
                code = Convert.ToInt32(StatusEnum.Failed)
            };

            result.url = "/Login/Index";
            try
            {
                #region 数据验证 (后期可改为模型验证)
                //非空
                if (String.IsNullOrWhiteSpace(loginInfo.Phone) && String.IsNullOrWhiteSpace(loginInfo.Name))
                {
                    result.msg = "请输入用户名或密码";
                    return(Ok(result));
                }
                if (String.IsNullOrWhiteSpace(loginInfo.Password))
                {
                    result.msg = "请输入用户名或密码";
                    return(Ok(result));
                }
                //防sql注入,后期补上
                #endregion
                //用户名查询


                //用户名查询用户信息
                AU_User userModel = UserService.GetUserInfoByNameOrPhone(loginInfo.Name);
                if (userModel == null)
                {
                    result.msg = "账号或密码错误";
                    return(Ok(result));
                }

                var token = BaseController.EncodeText(JsonConvert.SerializeObject(userModel));
                //密码判断
                string passWord = Encoding.UTF8.GetString(BaseController.AESDecrypt(Convert.FromBase64String(userModel.Pwd)));
                passWord = passWord.Replace("\0", "").Trim();
                if (passWord != loginInfo.Password)
                {
                    result.msg = "账号或密码错误";
                    return(Ok(result));
                }
                if (userModel.IsLogin == 1)
                {
                    result.msg = "用户已登录";
                    return(Ok(result));
                }
                //检查用户状态

                //加密存入缓存
                //// CacheHelper.SetAbsolute("token",token,15*60);
                //HttpContext.Response.Cookies.Delete("tokens");
                RedisManager.redisHelp.SetValue(userModel.Name, JsonConvert.SerializeObject(userModel), 20);
                HttpContext.Response.Cookies.Append("tokens", token);
                result.code = Convert.ToInt32(StatusEnum.Succeed);
                result.url  = "/Login/Index";
                result.msg  = "登录成功!";
                //是否需要返回登录成功后的实体
                return(Ok(result));
            }
            catch (Exception ex)
            {
                result.code = Convert.ToInt32(StatusEnum.Error);
                result.msg  = "内部请求出错";//内部请求出错
                LogHelp.Error(ex, ex.Message);
            }
            return(Ok(result));
        }