Beispiel #1
0
        public IActionResult syncOrg([FromBody] JObject[] value)
        {
            //List<Dictionary<string, object>> f = new List<Dictionary<string, object>>();
            //foreach (JObject item in value)
            //{
            //    var d = UTILITY.JsonConversionExtensions.ToDictionary(item);
            //    f.Add((Dictionary<string, object>)d);
            //}
            if (cm.getConfig())
            {
                try
                {
                    var t   = UTILITY.JsonConversionExtensions.ToDictionary(value);
                    var res = mm.syncOrg((List <Dictionary <string, object> >)t);
                    if (res == "2000")
                    {
                        Dictionary <string, object> d = new Dictionary <string, object>();
                        d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                        d["RECEIVE_URL"]  = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
                        d["SYNC_CONTENT"] = "云组织同步接收成功";
                        d["SYNC_RESULT"]  = 0;
                        d["ERROR_INFO"]   = "";
                        d["FAIL_CONTENT"] = "";
                        d["REMARK"]       = "";
                        srm.createSyncResult(d);
                    }
                    else
                    {
                        Dictionary <string, object> d = new Dictionary <string, object>();
                        d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                        d["RECEIVE_URL"]  = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();
                        d["SYNC_CONTENT"] = "云组织同步接收失败!" + res;
                        d["SYNC_RESULT"]  = 0;
                        d["ERROR_INFO"]   = "";
                        d["FAIL_CONTENT"] = "";
                        d["REMARK"]       = "";
                        srm.createSyncResult(d);
                    }

                    return(Content(res));
                }
                catch (Exception ex)
                {
                    return(Content(ex.ToString()));
                }
            }
            return(Content("5000"));
        }
        public IActionResult apiLogin([FromBody] JObject value)
        {
            Dictionary <string, object> d = value.ToObject <Dictionary <string, object> >();
            string userCode    = d["userCode"] == null ? "" : d["userCode"].ToString();
            string password    = d["password"] == null ? "" : d["password"].ToString();
            string userId      = "";
            string userName    = "******";
            string accessToken = "";

            try
            {
                if (string.IsNullOrEmpty(userCode) || string.IsNullOrEmpty(password))
                {
                    //return Json(new { code = -1, message = "推送接口用户名或密码不能为空!" });
                    return(Content(""));
                }
                UserLoginModule um = new UserLoginModule();
                DataTable       dt = um.getUserInfoByName(userCode);
                if (dt == null || dt.Rows.Count == 0)
                {
                    //return Json(new { code = -1, message = "云同步用户不存在!" });
                    return(Content(""));
                }
                if (password != dt.Rows[0]["USER_PASS"].ToString())
                {
                    //return Json(new { code = -1, message = "云同步用户密码错误!" });
                    return(Content(""));
                }
                userId      = dt.Rows[0]["USER_ID"].ToString();
                userName    = dt.Rows[0]["USER_NAME"].ToString();
                accessToken = AccessTokenTool.GetAccessToken(userId);
                UIDP.UTILITY.AccessTokenTool.DeleteToken(userId);
                UIDP.UTILITY.AccessTokenTool.InsertToken(userId, accessToken, DateTime.Now.AddHours(1));
                log.Info(DateTime.Now, userId, userName, Extension.GetClientUserIp(Request.HttpContext), 2, "云组织数据同步", "", 1);
                return(Content(accessToken));
            }
            catch (Exception ex)
            {
                log.Info(DateTime.Now, userId, userName, Extension.GetClientUserIp(Request.HttpContext), 1, "云组织数据同步", ex.Message.Length > 120 ? ex.Message.Substring(0, 100) : ex.Message, 1);
                return(Content(""));
            }
        }
Beispiel #3
0
        public IActionResult pushOrgList([FromBody] JObject value)
        //public IActionResult pushOrgList()
        {
            // sync_list
            Dictionary <string, object> r = new Dictionary <string, object>();

            try
            {
                Dictionary <string, object> dd = value.ToObject <Dictionary <string, object> >();
                //var targetlist = sncm.getSyncConfList();
                //foreach (var item in targetlist)
                foreach (var item in (JArray)dd["sync_list"])
                {
                    if (item["SYNC_FLAG"] != null && item["SYNC_FLAG"].ToString() == "0")
                    {
                        //string loginUrl = "http://" + item["SERVER_IP"].ToString() + ":" + item["SERVER_PORT"].ToString() + "/LogIn/apiLogin";
                        string loginUrl = "http://" + item["SERVER_IP"].ToString() + ":" + item["SERVER_PORT"].ToString() + item["AUTHENTICATION_URL"].ToString();
                        //WebRequest req = WebRequest.Create("http://192.168.1.113:12345/LogIn/apiLogin");
                        //string loginUrl = "http://192.168.1.107:12345/LogIn/apiLogin";
                        WebRequest req = WebRequest.Create(loginUrl);
                        Dictionary <string, string> postData = new Dictionary <string, string>();
                        //postData["userCode"] = "ceshi02";
                        //postData["password"] = "******";
                        postData["userCode"] = item["USER_CODE"].ToString();
                        postData["password"] = item["USER_PASS"].ToString();
                        string jsonString    = JsonConvert.SerializeObject(postData);
                        byte[] objectContent = Encoding.UTF8.GetBytes(jsonString);
                        req.ContentLength = objectContent.Length;
                        req.ContentType   = "application/json";
                        req.Method        = "POST";
                        using (var stream = req.GetRequestStream())
                        {
                            stream.Write(objectContent, 0, objectContent.Length);
                            stream.Close();
                        }


                        var resp = req.GetResponse();
                        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                        {
                            string s = sr.ReadToEnd();
                            if (s != "")
                            {
                                //"/Org/syncOrg"
                                string syncUrl = "http://" + item["SERVER_IP"].ToString() + ":" + item["SERVER_PORT"].ToString() + item["SERVER_URL"].ToString();
                                //string syncUrl = "http://192.168.1.107:12345/Org/syncOrg";
                                //WebRequest pushreq = WebRequest.Create("http://192.168.1.113:12345/Org/syncOrg");
                                WebRequest pushreq           = WebRequest.Create(syncUrl);
                                DataTable  dt                = mm.fetchSyncOrgTable();
                                string     pushjsonString    = JsonConvert.SerializeObject(dt);
                                byte[]     pushobjectContent = Encoding.UTF8.GetBytes(pushjsonString);
                                pushreq.ContentLength = pushobjectContent.Length;
                                pushreq.ContentType   = "application/json";
                                pushreq.Headers.Add("X-Token", s.ToString());
                                pushreq.Method = "POST";
                                using (var stream = pushreq.GetRequestStream())
                                {
                                    stream.Write(pushobjectContent, 0, pushobjectContent.Length);
                                    stream.Close();
                                }
                                var pushresp = pushreq.GetResponse();
                                using (StreamReader pushsr = new StreamReader(pushresp.GetResponseStream()))
                                {
                                    string pushs = pushsr.ReadToEnd();
                                    if (pushs == "2000")
                                    {
                                        Dictionary <string, object> d = new Dictionary <string, object>();
                                        d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                                        d["RECEIVE_URL"]  = item["SERVER_IP"].ToString();
                                        d["SYNC_CONTENT"] = "云组织同步推送成功";
                                        d["SYNC_RESULT"]  = 0;
                                        d["ERROR_INFO"]   = "";
                                        d["FAIL_CONTENT"] = "";
                                        d["REMARK"]       = "";
                                        srm.createSyncResult(d);
                                        r["message"] = "云组织同步推送成功";
                                        r["code"]    = 2000;
                                        return(Json(r));
                                    }
                                    else if (pushs == "5000")
                                    {
                                        Dictionary <string, object> d = new Dictionary <string, object>();
                                        d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                                        d["RECEIVE_URL"]  = item["SERVER_IP"].ToString();
                                        d["SYNC_CONTENT"] = "云组织同步推送失败";
                                        d["SYNC_RESULT"]  = 0;
                                        d["ERROR_INFO"]   = "云组织同步推送失败!" + pushs;
                                        d["FAIL_CONTENT"] = "系统未启用云组织";
                                        d["REMARK"]       = "";
                                        srm.createSyncResult(d);
                                        r["code"]    = -1;
                                        r["message"] = "系统未启用云组织";
                                        return(Json(r));
                                    }
                                    else
                                    {
                                        Dictionary <string, object> d = new Dictionary <string, object>();
                                        d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                                        d["RECEIVE_URL"]  = item["SERVER_IP"].ToString();
                                        d["SYNC_CONTENT"] = "云组织同步推送失败";
                                        d["SYNC_RESULT"]  = 0;
                                        d["ERROR_INFO"]   = "云组织同步推送失败!" + pushs;
                                        d["FAIL_CONTENT"] = "同步服务发生异常";
                                        d["REMARK"]       = "";
                                        srm.createSyncResult(d);
                                    }
                                }
                            }
                            else
                            {
                                Dictionary <string, object> d = new Dictionary <string, object>();
                                d["SEND_URL"]     = Extension.GetClientUserIp(Request.HttpContext);
                                d["RECEIVE_URL"]  = item["SERVER_IP"].ToString();
                                d["SYNC_CONTENT"] = "云组织同步推送失败";
                                d["SYNC_RESULT"]  = 0;
                                d["ERROR_INFO"]   = "云组织同步推送失败!";
                                d["FAIL_CONTENT"] = "云组织同步用户账号或密码不正确";
                                d["REMARK"]       = "";
                                srm.createSyncResult(d);
                                r["code"]    = -1;
                                r["message"] = "云组织同步用户账号或密码不正确";
                                return(Json(r));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                r["code"]    = -1;
                r["message"] = ex.Message;
            }
            return(Json(r));
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            try
            {
                Microsoft.Extensions.Primitives.StringValues AccessToken;    //获取header中某一项的值
                context.HttpContext.Request.Headers.TryGetValue("X-Token", out AccessToken);
                ClientIp   = Extension.GetClientUserIp(Request.HttpContext);
                actionName = (context.RouteData.Values["action"]).ToString().ToLower();    //获取当前方法
                if (actionName == "loginconfig" || actionName == "getcolor")
                {
                    base.OnActionExecuting(context); return;
                }
                //根据实际需求进行具体实现
                accessToken = AccessToken;
                if (accessToken == "")
                {
                    context.Result = new ObjectResult(new { code = 50008, msg = "没有找到X-Token" });
                }
                string userId = UIDP.UTILITY.AccessTokenTool.GetUserId(AccessToken);
                UserId = userId;
                if (actionName == "info")
                {
                    UIDP.UTILITY.Message mes = UIDP.UTILITY.AccessTokenTool.IsInValidUser(userId, AccessToken, "user");
                    if (mes.code != 2000)
                    {
                        context.Result = new ObjectResult(mes);
                    }
                }
                else
                {
                    BIZModule.UserModule mm = new BIZModule.UserModule();
                    string admin            = mm.getAdminCode();
                    if (userId == admin)
                    {
                        UserName = "******";
                    }
                    else
                    {
                        UserName = mm.getUserInfoByUserId(userId).USER_NAME;
                    }
                    UIDP.UTILITY.Message mes = UIDP.UTILITY.AccessTokenTool.IsInValidUser(userId, AccessToken, admin);
                    if (mes.code != 2000)
                    {
                        context.Result = new ObjectResult(mes);
                    }
                }
                UIDP.LOG.SysLog log = new LOG.SysLog();
                log.Info(DateTime.Now, userId, UserName, ClientIp, 0, actionName, "", 1);
            }
            catch (Exception ex)
            {
                UIDP.LOG.SysLog log = new LOG.SysLog();
                log.Info(DateTime.Now, UserId, UserName, ClientIp, 1, actionName, ex.Message.Length > 120 ? ex.Message.Substring(0, 100) : ex.Message, 1);
                context.Result = new ObjectResult(new { code = -1, msg = "验证token时程序出错", result = ex.Message });
            }
            #region 备份原来的逻辑

            /*
             * public override void OnActionExecuting(ActionExecutingContext context)
             * {
             *  try
             *  {
             *      Microsoft.Extensions.Primitives.StringValues AccessToken;//获取header中某一项的值
             *      context.HttpContext.Request.Headers.TryGetValue("X-Token", out AccessToken);
             *
             *      actionName = (context.RouteData.Values["action"]).ToString().ToLower();//获取当前方法
             *      //根据实际需求进行具体实现
             *      accessToken = AccessToken;
             *      if (accessToken == "")
             *      {
             *          context.Result = new ObjectResult(new { code = 50008, msg = "没有找到X-Token" });
             *      }
             *      string userId = UIDP.UTILITY.AccessTokenTool.GetUserId(AccessToken);
             *      BIZModule.UserModule mm = new BIZModule.UserModule();
             *      string admin = mm.getAdminCode();
             *      if (userId == admin)
             *      {
             *          UserName = "******";
             *      }
             *      else
             *      {
             *          UserName = mm.getUserInfoByUserId(userId).USER_NAME;
             *      }
             *      UIDP.UTILITY.Message mes = UIDP.UTILITY.AccessTokenTool.IsInValidUser(userId, AccessToken,admin);
             *      if (mes.code != 2000)
             *      {
             *          context.Result = new ObjectResult(mes);
             *      }
             *      UserId = userId;
             *      ClientIp = Extension.GetClientUserIp(Request.HttpContext);
             *      UIDP.LOG.SysLog log = new LOG.SysLog();
             *      log.Info(DateTime.Now, userId, UserName, ClientIp, 0, actionName, "");
             *  }
             *  catch (Exception ex)
             *  {
             *      UIDP.LOG.SysLog log = new LOG.SysLog();
             *      log.Info(DateTime.Now, UserId, UserName, ClientIp, 1, actionName, ex.Message.Length > 120 ? ex.Message.Substring(0, 100) : ex.Message);
             *      context.Result = new ObjectResult(new { code = -1, msg = "验证token时程序出错", result = ex.Message });
             *  }
             *
             * }
             */
            #endregion
        }
        public IActionResult loginByUsernames([FromBody] JObject value)
        {
            string userId   = "";
            string userName = "";

            try
            {
                Dictionary <string, object> d = value.ToObject <Dictionary <string, object> >();
                string username = d["username"] == null ? "" : d["username"].ToString();
                string password = d["password"] == null ? "" : d["password"].ToString();
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    return(Json(new { code = -1, message = "用户名或密码不能为空!" }));
                }
                UserModule mm = new UserModule();
                userId = mm.getAdminCode();
                string pass = mm.getAdminPass();
                if ((username == userId))
                {
                    if (password != pass)
                    {
                        return(Json(new { code = -1, message = "管理员密码不正确!" }));
                    }
                    userName = "******";
                    string accessToken = AccessTokenTool.GetAccessToken(userId);
                    UIDP.UTILITY.AccessTokenTool.DeleteToken(userId);
                    UIDP.UTILITY.AccessTokenTool.InsertToken(userId, accessToken, DateTime.Now.AddHours(1));
                    log.Info(DateTime.Now, userId, userName, Extension.GetClientUserIp(Request.HttpContext), 2, "LogIn", "", 1);
                    return(Json(new
                    {
                        code = 2000,
                        message = "",
                        token = accessToken,
                        orgList = new DataTable(),
                        userList = new DataTable(),
                        roleLevel = "admin"
                    }));
                }
                else
                {
                    UserLoginModule um = new UserLoginModule();
                    if (d["userDomain"].ToString() == "PTR_IDENT")
                    {
                        var builder = new ConfigurationBuilder()
                                      .SetBasePath(Directory.GetCurrentDirectory())
                                      .AddJsonFile("appsettings.json");
                        Configuration = builder.Build();
                        string            LDAPPATH   = Configuration["LdapPath"];
                        DirectoryEntry    entry      = new DirectoryEntry(LDAPPATH, username, password);
                        DirectorySearcher mySearcher = new DirectorySearcher(entry);
                        mySearcher.Filter = "(SAMAccountName=" + username + ")";
                        SearchResult result = mySearcher.FindOne();
                        if (result == null)
                        {
                            throw new Exception("用户认证错误");
                        }
                        else
                        {
                            DataTable userdt = um.getUserInfoByName(username);
                            if (userdt == null || userdt.Rows.Count == 0)
                            {
                                return(Json(new { code = -1, message = "本地用户不存在,请同步用户信息!" }));
                            }
                            Dictionary <string, object> dinfo = new Dictionary <string, object>();
                            if (password != userdt.Rows[0]["USER_PASS"].ToString())
                            {
                                //dinfo["password"] = userdt.Rows[0]["USER_PASS"].ToString();
                                dinfo["newpassword"] = UIDP.Security.SecurityHelper.StringToMD5Hash(password);
                                dinfo["userid"]      = userdt.Rows[0]["USER_ID"].ToString();
                                mm.updatePTRpass(dinfo);
                            }
                        }
                    }

                    DataTable dt = um.getUserInfoByName(username);
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        return(Json(new { code = -1, message = "此用户不存在!" }));
                    }
                    password = UIDP.Security.SecurityHelper.StringToMD5Hash(password);
                    if (password != dt.Rows[0]["USER_PASS"].ToString())
                    {
                        return(Json(new { code = -1, message = "密码错误!" }));
                    }
                    userId = dt.Rows[0]["USER_ID"].ToString();
                    //userName = dt.Rows[0]["LOGIN_REMARK"].ToString();
                    string accessToken = AccessTokenTool.GetAccessToken(userId);
                    UIDP.UTILITY.AccessTokenTool.DeleteToken(userId);
                    UIDP.UTILITY.AccessTokenTool.InsertToken(userId, accessToken, DateTime.Now.AddHours(1));
                    DataTable dtUser = um.getLoginByID(userId);
                    int       level  = 1;
                    if (Extension.GetClientUserIp(Request.HttpContext).ToString() != dt.Rows[0]["USER_IP"].ToString())
                    {
                        level = 2;
                    }
                    log.Info(DateTime.Now, userId, userName, Extension.GetClientUserIp(Request.HttpContext), 2, "LogIn", "", level);
                    return(Json(new
                    {
                        code = 2000,
                        message = "",
                        token = accessToken,
                        orgList = new DataTable(),
                        userList = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dtUser)),
                        roleLevel = ""
                    }));
                }
            }
            catch (Exception ex)
            {
                log.Info(DateTime.Now, userId, userName, Extension.GetClientUserIp(Request.HttpContext), 1, "LogIn", ex.Message.Length > 120 ? ex.Message.Substring(0, 100) : ex.Message, 1);
                return(Json(new { code = -1, message = "登录时程序发生错误" + ex.Message }));
            }
        }