Пример #1
0
        public HttpResponseMessage UserInfo(string token)
        {
            string     result = string.Empty;
            RuleCommon rule   = new RuleCommon();

            try
            {
                tblFW_User objUser = new tblFW_User();

                //var strTicket = FormsAuthentication.Decrypt(token).UserData;

                IJsonSerializer   serializer = new JsonNetSerializer();
                IDateTimeProvider provider   = new UtcDateTimeProvider();
                IJwtValidator     validator  = new JwtValidator(serializer, provider);
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
                IJwtDecoder       decoder    = new JwtDecoder(serializer, validator, urlEncoder);
                string            json       = "[" + decoder.Decode(token, "YYplay", verify: true).ToString() + "]";//token为之前生成的字符串
                string            userName   = "";
                JArray            jsonObj    = JArray.Parse(json);
                string            password   = "";
                for (int i = 0; i < jsonObj.Count; i++)
                {
                    userName = jsonObj[i]["name"].ToString();
                    password = jsonObj[i]["pass"].ToString();
                }



                RuletblFW_User users = new RuletblFW_User();
                if (CheckUser(userName, password, "", ref objUser) == false)
                {
                    result = rule.JsonStr("error", "请重新登录,获取Token!", "");
                    return(new HttpResponseMessage {
                        Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                    });
                }



                LoginInfo lginfo = new LoginInfo();
                lginfo.userid       = objUser.fldAutoID.ToString();
                lginfo.roleid       = objUser.fldRoleID;
                lginfo.token        = "";
                lginfo.cityid       = objUser.fldCityID.ToString();
                lginfo.username     = objUser.fldUserName;
                lginfo.roles        = objUser.fldroles;
                lginfo.introduction = objUser.fldintroduction;
                lginfo.avatar       = objUser.fldavatar;
                result = rule.JsonStr("ok", "", lginfo);
                return(new HttpResponseMessage {
                    Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                });
            }
            catch (Exception e)
            {
                result = rule.JsonStr("error", e.Message, "");
                return(new HttpResponseMessage {
                    Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                });
            }
        }
        public HttpResponseMessage LoginMerchant(string username, string password)
        {
            string     result = string.Empty;
            RuleCommon rule   = new RuleCommon();

            try
            {
                string massge = "";
                //到数据库进行校验
                if (CheckUser(username, password, ref massge) == false)
                {
                    result = rule.JsonStr("error", massge, "");
                    return(new HttpResponseMessage {
                        Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                    });
                }
                DateTime          dtime    = DateTime.Parse(DateTime.Now.ToShortDateString());
                IDateTimeProvider provider = new UtcDateTimeProvider();
                var now               = provider.GetNow();
                var unixEpoch         = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
                var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
                var payload           = new Dictionary <string, object>
                {
                    { "exp", secondsSinceEpoch + 10000 },
                    { "name", username }
                };
                IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
                IJsonSerializer   serializer = new JsonNetSerializer();
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
                IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);
                var token = encoder.Encode(payload, "YYplayMerchant");

                ////返回登录结果、用户信息、用户验证票据信息
                //var Token = FormsAuthentication.Encrypt(token);
                ////将身份信息保存在session中,验证当前请求是否是有效请求
                //if (HttpContext.Current.Session[username] == null)
                //    HttpContext.Current.Session[username] = Token;
                LoginInfo lginfo = new LoginInfo();

                lginfo.token = token;

                result = rule.JsonStr("ok", "", lginfo);
                //插入登录日志
                CheckIP checkIP = new CheckIP();
                string  ip      = checkIP.GetIP();

                using (Model1 db = new Model1())
                {
                    tblMerchantLog tblMerchantLog = new tblMerchantLog();
                    tblMerchantLog.fldAutoID    = 0;
                    tblMerchantLog.fldLoginCity = "";
                    tblMerchantLog.fldLoginIP   = ip;
                    tblMerchantLog.fldLoginTime = DateTime.Now;
                    tblMerchantLog.fldMerchant  = username;
                    db.tblMerchantLog.Add(tblMerchantLog);
                    db.SaveChanges();
                }

                return(new HttpResponseMessage {
                    Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                });
            }
            catch (Exception e)
            {
                result = rule.JsonStr("error", e.Message, "");
                return(new HttpResponseMessage {
                    Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json")
                });
            }
        }