Exemplo n.º 1
0
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="Request">页面请求者</param>
        /// <param name="ID">部门ID</param>
        /// <returns>返回部门信息</returns>
        public static DepartmentInfo GetSelfDepartment(HttpRequest Request, string ID)
        {
            //string code = Request["code"];
            //部门信息
            DepartmentInfo departmentInfo = null;

            try
            {
                //根据CropId与cropSecret去换取AccessToken
                //这里的AccessToken的主要含义是企业令牌,它的意思是说依靠这个令牌可以去拿取与企业相关的数据,
                //根据官方文档介绍这里的有效期是7200秒,
                if (Config.TokenModel == null)
                {
                    //获取token
                    Config.TokenModel = EnterpriseBusiness.GetToken(Config.ECorpId, Config.ECorpSecret);
                }
                var access_token = Config.TokenModel.access_token;
                //---------------利用access_token和code去换取当前用户
                departmentInfo = EnterpriseBusiness.GetAparentMent(access_token, ID);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }

            return(departmentInfo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="Request">当前页面</param>
        public void GetConfig(HttpRequest Request)
        {
            try
            {
                _EnterPrise_MB = this;
                appId          = Config.EAgentID;
                corpId         = Config.ECorpId;
                string corpSecret = Config.ECorpSecret;
                string url        = Request.Url.AbsoluteUri;

                nonceStr  = Helper.randNonce();
                timestamp = Helper.timeStamp();
                //string url = Request.Url.ToString();
                //if (Config.TokenModel == null)
                //{
                //    //这里重新实现
                //    Config.TokenModel = EnterpriseBusiness.GetToken(corpId, corpSecret);
                //}
                //if (string.IsNullOrEmpty(jsApiTicket))
                //{
                if (Config.TokenModel != null && !string.IsNullOrEmpty(Config.TokenModel.access_token))
                {
                    jsApiTicket = EnterpriseBusiness.GetTickets(Config.TokenModel.access_token);
                }
                //}

                string jsApiTicket_Message = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsApiTicket, nonceStr, timestamp, url);

                //string jsApiTicket_Message = string.Format("nonce:{0},timestamp:{1},url:{2},ticket:{3}", nonceStr, timestamp, url, jsApiTicket);

                signature = FormsAuthentication.HashPasswordForStoringInConfigFile(jsApiTicket_Message, "SHA1").ToLower();

                //GenSigurate(nonceStr, timestamp, jsApiTicket, url, ref signature);
                // 这里参数的顺序要按照 key 值 ASCII 码升序排序
                //string rawstring = "{Keys.jsapi_ticket}=" +jsApiTicket
                //                 + "&{Keys.noncestr}=" + nonceStr
                //                 + "&{Keys.timestamp}=" + timestamp
                //                 + "&{Keys.url}=" + url;
                // signature = SignPackageHelper.Sha1Hex(rawstring).ToLower();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="Request"></param>
        /// <returns></returns>
        public static string GetSelfInfo(HttpRequest Request)
        {
            //返回的用户信息

            string userInfo = null;

            try
            {
                string code = Request["code"];

                //是否强制获取,在这里默认为不强制获取
                bool   isNeedForceGet    = false;
                string strIsNeedForceGet = Request["isFoceGet"];
                if (!string.IsNullOrEmpty(strIsNeedForceGet))
                {
                    isNeedForceGet = Convert.ToBoolean(strIsNeedForceGet);
                }


                string mode = Request["mode"];

                //根据CropId与cropSecret去换取AccessToken
                //这里的AccessToken的主要含义是企业令牌,它的意思是说依靠这个令牌可以去拿取与企业相关的数据,
                //根据官方文档介绍这里的有效期是7200秒,
                if (Config.TokenModel == null)
                {
                    Config.TokenModel = EnterpriseBusiness.GetToken(Config.ECorpId, Config.ECorpSecret);
                }
                //暂时停止
                if (timer == null)
                {
                    timer = new Timer()
                    {
                        Interval = 6000 * 1000
                    };
                    timer.Elapsed += timer_Elapsed;
                    timer.Start();
                }
                string access_token = Config.TokenModel.access_token;

                /*
                 * 这里拿到企业令牌后,可以将其保存到数据库中,同时设定它的过期时间为当前时间+7200秒,
                 * 每次使有令牌时判断当前时间是否已经超过了有效期,如果超过了有效期,请重新获取新的令牌
                 * 为了安全access_token在实际的开发过程当中不建议放到客户端,这个令牌一般禁止用户接触到,一般可放在服务器端的session里
                 */
                //---------------利用access_token和code去换取当前用户
                UserModel userModel = EnterpriseBusiness.GetCurrentUser(access_token, code);
                //先从缓存池里进行获取
                if (dicUserInfo.ContainsKey(userModel.userid) && !isNeedForceGet)
                {
                    userInfo = dicUserInfo[userModel.userid];
                }
                else
                {
                    if (string.IsNullOrEmpty(mode))
                    {
                        mode = "pc";
                    }

                    //获取用户信息
                    userInfo = EnterpriseBusiness.GetUserInfoByString(access_token, userModel.userid);

                    if (!string.IsNullOrEmpty(userInfo))
                    {
                        UserInfo userIn = JsonConvert.DeserializeObject <UserInfo>(userInfo);
                        if (userInfo != null && !string.IsNullOrEmpty(userIn.name))
                        {
                            //LogManage.WriteLog(typeof(EnterpriseBusiness), mode + "钉钉免登:" + userIn.name);

                            dicUserInfo.Add(userModel.userid, userInfo);
                        }
                        else
                        {
                            ErrorDealWith(mode);
                        }
                    }
                    else
                    {
                        ErrorDealWith(mode);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(userInfo);
        }