Esempio n. 1
0
        public string Testone()
        {
            //载荷(payload)
            var payload = new Dictionary <string, object>
            {
                { "iss", "流月无双" },                                                   //发行人
                { "exp", DateTimeOffset.UtcNow.AddSeconds(60).ToUnixTimeSeconds() }, //到期时间
                { "sub", "testJWT" },                                                //主题
                { "aud", "USER" },                                                   //用户
                { "iat", DateTime.Now.ToString() },                                  //发布时间
                { "data", new { name = "111", age = 11, address = "hubei" } }
            };

            //生成JWT
            Console.WriteLine("******************生成JWT*******************");
            string JWTString = JwtHelper.CreateJWT(payload);

            Console.WriteLine(JWTString);

            //var aa = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiLmtYHmnIjml6Dlj4wiLCJleHAiOjE1ODQ0NTA3MTAsInN1YiI6InRlc3RKV1QiLCJhdWQiOiJVU0VSIiwiaWF0IjoiMjAyMC8zLzE3IDIxOjExOjQwIiwiZGF0YSI6eyJuYW1lIjoiMTExIiwiYWdlIjoxMSwiYWRkcmVzcyI6Imh1YmVpIn19.4N1yiHguaAkaiNaqzJppKXpunpJskQ-BZ415xs675ZI";
            //var bb = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiLmtYHmnIjml6Dlj4wiLCJleHAiOjE1ODQ0NTA3NDYsInN1YiI6InRlc3RKV1QiLCJhdWQiOiJVU0VSIiwiaWF0IjoiMjAyMC8zLzE3IDIxOjEyOjE3IiwiZGF0YSI6eyJuYW1lIjoiMTExIiwiYWdlIjoxMSwiYWRkcmVzcyI6Imh1YmVpIn19.dTGZLAomm9Zpx-502ktiGy_yARiJ6nWuWFh0ml_atyc";
            ////校验JWT
            //Console.WriteLine("*******************校验JWT,获得载荷***************");
            //string ResultMessage="";//需要解析的消息
            //string Payload;//获取负载
            //if (JwtHelper.ValidateJWT(JWTString, out Payload, out ResultMessage))
            //{
            //    Console.WriteLine(Payload);
            //}
            //Console.WriteLine(ResultMessage);//验证结果说明
            //Console.WriteLine("*******************END*************************");
            return(JWTString);
        }
Esempio n. 2
0
        public ActionResult <string> ExeAction(string Action, Object PostData)
        {
            Model.Action = Action;
            var           context     = _accessor.HttpContext;
            var           tokenHeader = context.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            TokenModelJWT tokenModel  = JwtHelper.SerializeJWT(tokenHeader);

            if (new DateTimeOffset(DateTime.Now.AddMinutes(5)).ToUnixTimeSeconds() > tokenModel.Exp)
            {
                //需要更新Token
                Model.uptoken = JwtHelper.CreateJWT(tokenModel.UserName, "Admin");
            }
            JH_Auth_UserB.UserInfo UserInfo = CacheHelp.Get(tokenModel.UserName) as JH_Auth_UserB.UserInfo;
            if (UserInfo == null)
            {
                UserInfo = new JH_Auth_UserB().GetUserInfo(10334, tokenModel.UserName);
                CacheHelp.Set(tokenModel.UserName, UserInfo);
            }
            try
            {
                JObject JsonData = JObject.FromObject(PostData);
                string  P1       = JsonData["P1"] == null ? "" : JsonData["P1"].ToString();
                string  P2       = JsonData["P2"] == null ? "" : JsonData["P2"].ToString();


                // 1.Load(命名空间名称),GetType(命名空间.类名)
                Type type = Assembly.Load("QJY.API").GetType("QJY.API." + Action.Split('_')[0].ToUpper() + "Manage");
                //2.GetMethod(需要调用的方法名称)
                MethodInfo method = type.GetMethod(Action.Split('_')[1].ToUpper());
                // 3.调用的实例化方法(非静态方法)需要创建类型的一个实例
                object obj = Activator.CreateInstance(type);
                //4.方法需要传入的参数
                object[] parameters = new object[] { JsonData, Model, P1, P2, UserInfo };
                method.Invoke(obj, parameters);
                new JH_Auth_LogB().InsertLog(Model.Action, "--调用接口", "", UserInfo.User.UserName, UserInfo.User.UserRealName, UserInfo.QYinfo.ComId, "");
            }
            catch (Exception ex)
            {
                Model.ErrorMsg = Action + "接口调用失败,请检查日志";
                Model.Result   = ex.ToString();
                new JH_Auth_LogB().InsertLog(Action, Model.ErrorMsg + ex.StackTrace.ToString(), ex.ToString(), tokenModel.UserName, "", 0, "");
            }

            return(ControHelp.CovJson(Model));
        }
Esempio n. 3
0
        public AjaxResult test2()
        {
            AuthInfo authInfo = new AuthInfo
            {
                IsAdmin = true,
                Roles   = new List <string> {
                    "admin", "owner"
                },
                UserName = "******"
            };

            IJwtAlgorithm     algorithm    = new HMACSHA256Algorithm();
            IJsonSerializer   serializer   = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder   = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder      = new JwtEncoder(algorithm, serializer, urlEncoder);
            string            encryptToken = encoder.Encode(authInfo, "123456");



            IJwtDecoder decoder = new JwtDecoder(serializer, urlEncoder);
            string      result  = decoder.Decode(encryptToken);


            string token = JwtHelper.CreateJWT(authInfo, "123456");

            AuthInfo tokenAuthInfo = JwtHelper.DecodeJWT <AuthInfo>(token, "123456");



            return(new AjaxResult
            {
                statusCode = 200,
                data = new {
                    authInfo = authInfo,
                    encryptToken = encryptToken,
                    result = JsonHelper.Deserialize <AuthInfo>(result),
                    token = token,
                    tokenAuthInfo = tokenAuthInfo
                }
            });
        }
Esempio n. 4
0
        public ActionResult <string> Login(Object PostData)
        {
            JObject JsonData = JObject.FromObject(PostData);
            string  username = JsonData["UserName"] == null ? "" : JsonData["UserName"].ToString();
            string  password = JsonData["password"] == null ? "" : JsonData["password"].ToString();
            Dictionary <string, string> results3 = JsonConvert.DeserializeObject <Dictionary <string, string> >(PostData.ToString());

            Model.ErrorMsg = "";
            JH_Auth_QY qyModel = new JH_Auth_QYB().GetALLEntities().First();

            password = CommonHelp.GetMD5(password);
            JH_Auth_User        userInfo = new JH_Auth_User();
            List <JH_Auth_User> userList = new JH_Auth_UserB().GetEntities(d => (d.UserName == username || d.mobphone == username) && d.UserPass == password).ToList();

            if (userList.Count() == 0)
            {
                Model.ErrorMsg = "用户名或密码不正确";
            }
            else
            {
                userInfo = userList[0];
                if (userInfo.IsUse != "Y")
                {
                    Model.ErrorMsg = "用户被禁用,请联系管理员";
                }
                if (Model.ErrorMsg == "")
                {
                    Model.Result  = JwtHelper.CreateJWT(username, "Admin");
                    Model.Result1 = userInfo.UserName;
                    Model.Result2 = qyModel.FileServerUrl;
                    Model.Result4 = userInfo;

                    CacheHelp.Remove(userInfo.UserName);
                }
            }


            return(ControHelp.CovJson(Model));;
        }
Esempio n. 5
0
        public ActionResult <string> ExeAction(string Action, Object PostData)
        {
            Model.Action = Action;
            var           context     = _accessor.HttpContext;
            var           tokenHeader = context.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            TokenModelJWT tokenModel  = JwtHelper.SerializeJWT(tokenHeader);

            if (new DateTimeOffset(DateTime.Now.AddMinutes(5)).ToUnixTimeSeconds() > tokenModel.Exp)
            {
                //需要更新Token
                Model.uptoken = JwtHelper.CreateJWT(tokenModel.UserName, "Admin");
            }
            JH_Auth_UserB.UserInfo UserInfo = CacheHelp.Get(tokenModel.UserName) as JH_Auth_UserB.UserInfo;
            if (UserInfo == null)
            {
                UserInfo = new JH_Auth_UserB().GetUserInfo(10334, tokenModel.UserName);
                CacheHelp.Set(tokenModel.UserName, UserInfo);
            }
            try
            {
                JObject JsonData = JObject.FromObject(PostData);
                string  P1       = JsonData["P1"] == null ? "" : JsonData["P1"].ToString();
                string  P2       = JsonData["P2"] == null ? "" : JsonData["P2"].ToString();

                //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString());
                var function = Activator.CreateInstance(typeof(AuthManage)) as AuthManage;
                var method   = function.GetType().GetMethod(Action.ToUpper());
                method.Invoke(function, new object[] { JsonData, Model, P1, P2, UserInfo });
                new JH_Auth_LogB().InsertLog(Model.Action, "--调用接口", "", UserInfo.User.UserName, UserInfo.User.UserRealName, UserInfo.QYinfo.ComId, "");
            }
            catch (Exception ex)
            {
                Model.ErrorMsg = Action + "接口调用失败,请检查日志";
                Model.Result   = ex.ToString();
                new JH_Auth_LogB().InsertLog(Action, Model.ErrorMsg + ex.StackTrace.ToString(), ex.ToString(), tokenModel.UserName, "", 0, "");
            }

            return(ControHelp.CovJson(Model));
        }
Esempio n. 6
0
        public ActionResult <string> GetUserCodeByCode()
        {
            try
            {
                var context = _accessor.HttpContext;

                #region 获取Code
                Model.ErrorMsg = "获取Code错误,请重试";

                string strCode      = context.Request.Query["code"].ToString();
                string strCorpID    = context.Request.Query["corpid"].ToString();
                string strModelCode = context.Request.Query["funcode"].ToString();

                if (!string.IsNullOrEmpty(strCode))
                {
                    var qy = new JH_Auth_QYB().GetEntity(p => p.corpId == strCorpID);
                    if (qy != null)
                    {
                        try
                        {
                            //通过微信接口获取用户名
                            WXHelp wx       = new WXHelp(qy);
                            string username = wx.GetUserDataByCode(strCode, strModelCode);
                            if (!string.IsNullOrEmpty(username))
                            {
                                var jau = new JH_Auth_UserB().GetUserByUserName(qy.ComId, username);

                                if (jau != null)
                                {
                                    Model.ErrorMsg = "";
                                    Model.Result   = JwtHelper.CreateJWT(username, "Admin");
                                    Model.Result1  = jau.UserName;
                                    Model.Result3  = qy.FileServerUrl;
                                }
                            }
                            else
                            {
                                Model.ErrorMsg = "当前用户不存在";
                            }
                        }
                        catch (Exception ex)
                        {
                            Model.ErrorMsg = ex.ToString();
                        }
                    }
                    else
                    {
                        Model.ErrorMsg = "当前企业号未在电脑端注册";
                    }
                }
                else
                {
                    Model.ErrorMsg = "Code为空";
                }
                #endregion
            }
            catch (Exception ex)
            {
                Model.ErrorMsg = "接口调用失败,请检查日志";
                Model.Result   = ex.ToString();
                CommonHelp.WriteLOG(ex.ToString());
            }
            return(ControHelp.CovJson(Model));
        }