Esempio n. 1
0
        public static wp_user CreateOrUpdate(string username, string password)
        {
            using (var ctx = new DatabaseContext(Config.DB_CONNECTION_STRING))
            {
                var user = ctx.WP_Users.FirstOrDefault(x => x.user_login == username);

                if (user != null)
                {
                    user.user_pass = password;
                }
                else
                {
                    user = new wp_user
                    {
                        user_login = username,
                        user_pass  = password
                    };

                    ctx.WP_Users.Add(user);
                }

                ctx.SaveChanges();

                return(user);
            }
        }
Esempio n. 2
0
        protected string CreateJWT(wp_user userInfo, Tenant tenant, string tenantId, bool rememberMe)
        {
            var      privateKey  = ((tenant != null) && !string.IsNullOrEmpty(tenant.PrivateKey)) ? tenant.PrivateKey : tenantId;
            var      securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(privateKey));
            var      credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
            DateTime jwtExpires  = DateTime.Now.AddMinutes(30);
            int      jwtDuration = 15;

            if (rememberMe)
            {
                int.TryParse(_configuration["Jwt:Expires"], out jwtDuration);
            }
            jwtExpires = DateTime.UtcNow.Add(TimeSpan.FromMinutes(jwtDuration));

            var token = new JwtSecurityToken(
                _configuration["Jwt:Issuer"],
                tenantId,
                new[]
            {
                new Claim(ClaimTypes.Name, userInfo.user_login)
            },
                expires: jwtExpires,
                signingCredentials: credentials);

            token.Header.Add("kid", tenantId);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Esempio n. 3
0
        private static void SaveShops(wp_user user, MERCHANT merchant, Func <string, MERCHANT, IEnumerable <Shop> > callback, string file)
        {
            log.DebugFormat("Getting shops for <{0}>.", merchant);
            var shops = callback(file, merchant);

            log.DebugFormat("Parsing shops for <{0}>.", merchant);
            SaveShops(user, shops, merchant);
        }
Esempio n. 4
0
        private static void SaveShops(wp_user user, IEnumerable <Shop> shops, MERCHANT merchant)
        {
            log.DebugFormat("Saving shops for <{0}>.", merchant);

            ShopCreationService.Save(user, shops.ToArray());

            log.DebugFormat("Finished saving shops for <{0}>.", merchant);
        }
        /* Public Methods. */

        public static void GetDeals(wp_user user)
        {
            log.Info("Getting deals for Shop Window.");
            GetDeals(MERCHANT.KGB, user);
            GetDeals(MERCHANT.LIVING_SOCIAL, user);
            GetDeals(MERCHANT.MIGHTY_DEALS, user);
            GetDeals(MERCHANT.WOWCHER, user);
        }
Esempio n. 6
0
        private static void SaveOffers(wp_user user, MERCHANT merchant, Func <string, MERCHANT, IEnumerable <DailyOffer> > callback, string file)
        {
            log.DebugFormat("Getting deals for <{0}>.", merchant);
            var dailyOffers = callback(file, merchant);

            log.DebugFormat("Parsing deals for <{0}>.", merchant);
            SaveOffers(user, dailyOffers, merchant);
        }
Esempio n. 7
0
        public static wp_user GetPost(string id, string connectionString, IDbConnection connection, IDbTransaction transaction)
        {
            wp_user result = null;

            var _connection = GetConnection(connection, connectionString);

            var sQuery = "SELECT TOP 10 * FROM wp_user WHERE (post_name = @post_name) ";

            result = _connection.Query <wp_user>(sQuery, new
            {
                post_name = id
            }, transaction: transaction).FirstOrDefault();

            return(result);
        }
Esempio n. 8
0
        public static wp_user Login(string id, string email, string password, string connectionString, IDbConnection connection, IDbTransaction transaction)
        {
            wp_user result = null;

            var _connection = GetConnection(connection, connectionString);

            var sQuery = "SELECT * FROM wp_user WHERE ( (user_login = @email) AND (user_pass = @password) ) ";

            result = _connection.Query <wp_user>(sQuery, new
            {
                email    = email,
                password = password,
            }, transaction: transaction).FirstOrDefault();

            return(result);
        }
Esempio n. 9
0
        /* Private. */

        private static void SaveOffers(wp_user user, IEnumerable <DailyOffer> dailyOffers, MERCHANT merchant)
        {
            log.DebugFormat("Saving deals for <{0}>.", merchant);

            try
            {
                DailyOfferCreationService.Save(user, dailyOffers.ToArray());
            }
            catch (Exception e)
            {
                log.FatalFormat(e.Message);
                log.FatalFormat(e.StackTrace);
                throw;
            }

            log.DebugFormat("Finished saving deals for <{0}>.", merchant);
        }
Esempio n. 10
0
        protected void Page_Load(object sender, EventArgs e)
        {
            vote_id = RequestHelper.GetQueryInt("vote_id", 0);
            //vote_id = 4;
            int count = RequestHelper.GetQueryInt("count", 20);

            is_validity = false;
            vote        = new Bll.WeChat.wp_shop_votebll().GetItem(vote_id);
            list        = new Bll.WeChat.wp_shop_vote_optionbll().GetOptionListByVoteId(vote_id, count);
            url         = Request.Url.ToString();
            if (vote != null)
            {
                var now = BaseClass.ConvertDataTimeToLong(DateTime.Now);
                is_validity = vote.start_time <now && vote.end_time> now;
            }
            if (!string.IsNullOrEmpty(openId))
            {
                user = new Bll.WeChat.wp_userbll().GetUserInfoByOpenId(openId);
                //LogHandler.Info($"votelist页,openId{openId},user:{JsonHelper.Serialize(user)}");
                if (user != null)
                {
                    list_log = new Bll.WeChat.wp_shop_vote_logbll().GetVoteLogByVoteId(user.Id, vote_id);
                }
            }

            if (list != null && list.Count > 0 && list_log != null && list_log.Count > 0)
            {
                list.ForEach(s =>
                {
                    s.IsVote = list_log.Count >= vote.multi_num;
                    //限制每个一天只能投一票
                    //s.IsVoteCurrent = list_log.Any(l => l.uid == user.Id && l.option_id == s.Id);
                    //不限制每个一天只能投一票
                    s.IsVoteCurrent = false;
                });
            }
            if (list != null && list.Count > 0)
            {
                list.ForEach(s =>
                {
                    var tmp     = new Bll.WeChat.wp_picturebll().GetItem((int)s.image);
                    s.ImagePath = tmp == null ? "" : tmp.path;
                });
            }
        }
Esempio n. 11
0
        public static int?register(string firstname, string lastname, string email, string password, string connectionString, IDbConnection connection, IDbTransaction transaction)
        {
            int?result = null;

            var _connection = GetConnection(connection, connectionString);
            var username    = string.Format("{0}-{1}", firstname.Trim().ToLower(), lastname.Trim().ToLower());
            var user        = new wp_user()
            {
                first_name          = firstname, last_name = lastname, user_email = email, user_pass = password,
                user_activation_key = Guid.NewGuid().ToString(),
                display_name        = username, user_login = email, user_nicename = username,
                user_registered     = DateTime.Now, user_status = 1, user_url = ""
            };

            result = _connection.Insert <wp_user>(user, transaction: transaction);

            return(result);
        }
Esempio n. 12
0
        public IActionResult Login([FromHeader] String username, [FromHeader] string password, [FromHeader] bool rememberme)
        {
            wp_user user   = null;
            var     tenant = this.GetTenant();

            // Validate that this user is authentic and is authorized to access your system
            // TODO: Implement your own authetication logic
            if (tenant != null)
            {
                user = Users.Login("", username, password, tenant.ConnectionString, null, null);
                if (user != null)
                {
                    var token = this.CreateJWT(user, tenant, tenant.Key, rememberme);
                    return(Ok(new { token = token }));
                }
            }

            return(BadRequest("you are not logged-in "));
        }
Esempio n. 13
0
        /* Public Methods. */

        public static String Execute(
            MERCHANT merchant,
            wp_user user,
            Func <MERCHANT, string> getDataFeed,
            Func <string, MERCHANT, IEnumerable <DailyOffer> > getOffers,
            Func <string, MERCHANT, IEnumerable <Shop> > getShops = null)
        {
            var file = Download(merchant, getDataFeed);

            if (getShops != null)
            {
                SaveShops(user, merchant, getShops, file);
            }

            if (getOffers != null)
            {
                SaveOffers(user, merchant, getOffers, file);
            }

            return(file);
        }
Esempio n. 14
0
        /* Public Methods. */

        public static void Save(wp_user user, params DailyOffer[] offers)
        {
            foreach (var offer in offers)
            {
                if (!PostCreationService.Contains(offer.DatabaseIdentifier))
                {
                    var terms        = offer.GetTerms();
                    var customFields = offer.GetCustomFields();

                    var id = PostCreationService.CreatePost(user, offer.Title, offer.DatabaseIdentifier, offer.Description, "code");

                    PostCreationService.CreatePostMetadata(id, customFields);
                    PostCreationService.CreatePostTaxonomy(id, terms);

                    log.DebugFormat("Created daily offer with ID <{0}> for merchant <{1}>.", offer.UniqueId, offer.Merchant);
                }
                else
                {
                    log.DebugFormat("Skipping daily offer with ID <{0}> for merchant <{1}>.", offer.UniqueId, offer.Merchant);
                }
            }
        }
Esempio n. 15
0
        public static long CreatePost(wp_user user, string title, string dbid, string description, string type)
        {
            using (var ctx = new DatabaseContext(Config.DB_CONNECTION_STRING))
            {
                var post = new wp_post
                {
                    post_title            = title,
                    post_type             = type,
                    post_name             = dbid,
                    post_status           = "publish",
                    post_content          = description,
                    post_excerpt          = String.Empty,
                    to_ping               = String.Empty,
                    pinged                = String.Empty,
                    post_content_filtered = String.Empty,
                    comment_status        = "open",
                    ping_status           = "open",
                    post_password         = String.Empty,
                    post_parent           = 0,
                    guid              = String.Empty,
                    menu_order        = 0,
                    post_mime_type    = String.Empty,
                    comment_count     = 0,
                    post_author       = user.ID,
                    post_date         = DateTime.Now,
                    post_date_gmt     = DateTime.Now,
                    post_modified     = DateTime.Now,
                    post_modified_gmt = DateTime.Now
                };

                ctx.WP_Posts.Add(post);

                ctx.SaveChanges();

                return(post.ID);
            }
        }
        public static void Save(wp_user user, params Shop[] shops)
        {
            if (shops != null)
            {
                foreach (var shop in shops)
                {
                    if (!Contains(shop))
                    {
                        var id = PostCreationService.CreatePost(user, shop.Title, shop.DatabaseIdentifier, shop.Description, "shop");

                        PostCreationService.CreatePostMetadata(id, shop.GetCustomFields());
                        PostCreationService.CreatePostTaxonomy(id, new[] { new wp_term_relationship {
                                                                               term_taxonomy_id = TaxonomyCreationService.GetGeography(shop.Geography)
                                                                           } });

                        log.DebugFormat("Created shop with ID <{0}> for source <{1}>.", shop.UniqueId, shop.Source);
                    }
                    else
                    {
                        log.DebugFormat("Skipping shop with ID <{0}> for source <{1}>.", shop.UniqueId, shop.Source);
                    }
                }
            }
        }
Esempio n. 17
0
 public static void GetDeals(wp_user user)
 {
     log.Info("Getting deals for Amazon.");
     log.InfoFormat("Using value <{0}> for the configuration key DB_CONNECTION_STRING.", Config.DB_CONNECTION_STRING);
     AbstractProvider.Execute(MERCHANT.AMAZON, user, DataFeed.Download, Parser.GetDeals, Parser.GetShops);
 }
Esempio n. 18
0
 public static void GetDeals(MERCHANT merchant, wp_user user)
 {
     AbstractProvider.Execute(merchant, user, DataFeed.Download, Parser.GetDeals);
 }
Esempio n. 19
0
        protected override void OnInit(EventArgs e)
        {
            //启用微信打开控制
            var openIdCookie = HttpContext.Current.Request.Cookies[BaseClass.OpenId_Cookie];
            var openId       = openIdCookie == null ? "" : openIdCookie.Value;

            var uIdCookie = HttpContext.Current.Request.Cookies[BaseClass.Uid_Cookie];

            uId = uIdCookie == null ? 0: BitAuto.Utils.ConvertHelper.GetInteger(uIdCookie.Value);
            LogHandler.Info("uid:" + uId);
            //if (string.IsNullOrEmpty(openId))
            if (uId == 0)
            {
                if (BaseClass.IsNeedWeiXin)
                {
                    string returnurl = "/";
                    returnurl = Request.Url?.ToString();
                    string code         = RequestHelper.GetQueryString("code");
                    string state        = RequestHelper.GetQueryString("state");
                    string redirect_uri = RequestHelper.GetQueryString("redirect_uri");
                    #region MyRegion
                    //021QRK2o05U3Lr1f2tZn0Tdt2o0QRK26
                    //http://sfl.sanfengli.cn/home/votelist.aspx?vote_id=8&code=021QRK2o05U3Lr1f2tZn0Tdt2o0QRK26&state=http:%2F%2Fsfl.sanfengli.cn%2Fhome%2Fvotelist.aspx%3Fvote_id%3D8&from=groupmessage&isappinstalled=0,api
                    //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6c4c8bb521e45019&redirect_uri=http%3A%2F%2Fsfl.sanfengli.cn%2Fhome%2Fvotelist.aspx%3Fvote_id%3D8%26from%3Dgroupmessage&response_type=code&scope=snsapi_userinfo&state=http%3A%2F%2Fsfl.sanfengli.cn%2Fhome%2Fvotelist.aspx%3Fvote_id%3D8%26from%3Dgroupmessage&connect_redirect=1#wechat_redirect
                    //http://sfl.sanfengli.cn/home/votelist.aspx?vote_id=8&code=021QRK2o05U3Lr1f2tZn0Tdt2o0QRK26&state=http:%2F%2Fsfl.sanfengli.cn%2Fhome%2Fvotelist.aspx%3Fvote_id%3D8&from=groupmessage&isappinstalled=0,code=021QRK2o05U3Lr1f2tZn0Tdt2o0QRK26,state=http://sfl.sanfengli.cn/home/votelist.aspx?vote_id=8

                    //string from = RequestHelper.GetQueryString("from");
                    //if (!string.IsNullOrEmpty(code)&&string.Equals(code, "021QRK2o05U3Lr1f2tZn0Tdt2o0QRK26", StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(state))
                    //{
                    //    OAuthScope scope;
                    //    if (IsNeedUserInfo)
                    //    {
                    //        scope = OAuthScope.snsapi_userinfo;
                    //    }
                    //    else
                    //    {
                    //        scope = OAuthScope.snsapi_base;
                    //    }
                    //    string oauthUrl = Weixin.OauthUrl(state, state, scope);
                    //    LogHandler.Info($"from:{from},code:{code},statue:{state},oauthUrl:{oauthUrl}" );
                    //    Response.Redirect(oauthUrl);
                    //}
                    #endregion

                    if (string.IsNullOrWhiteSpace(code))
                    {
                        OAuthScope scope;
                        if (IsNeedUserInfo)
                        {
                            scope = OAuthScope.snsapi_userinfo;
                        }
                        else
                        {
                            scope = OAuthScope.snsapi_base;
                        }
                        string oauthUrl = Weixin.OauthUrl(Request.Url?.ToString(), "", scope);
                        LogHandler.Info("code为空,oauthUrl:" + oauthUrl);
                        Response.Redirect(oauthUrl);
                    }
                    else
                    {
                        OAuthAccessTokenResult oAuthAccessTokenResult = null;

                        try
                        {
                            //通过,用code换取access_token

                            var isSecondRequest = false;
                            lock (OAuthCodeCollectionLock)
                            {
                                isSecondRequest = OAuthCodeCollection.ContainsKey(code);
                            }

                            if (!isSecondRequest)
                            {
                                //第一次请求
                                LogHandler.Info($"第一次微信OAuth到达,code:{code}");
                                lock (OAuthCodeCollectionLock)
                                {
                                    OAuthCodeCollection[code] = null;
                                }
                            }
                            else
                            {
                                //第二次请求
                                LogHandler.Info($"第二次微信OAuth到达,code:{code}");
                                lock (OAuthCodeCollectionLock)
                                {
                                    oAuthAccessTokenResult = OAuthCodeCollection[code];
                                }
                            }
                            try
                            {
                                oAuthAccessTokenResult = oAuthAccessTokenResult ?? OAuthApi.GetAccessToken(BaseClass.AppId, BaseClass.Secret, code);
                            }
                            catch (Exception ex)
                            {
                                LogHandler.Info($"微信网页授权api信息:{ex.Message}。请求Url:{Request.Url},api参数:url={returnurl},code={code},state={state}");
                            }

                            if (oAuthAccessTokenResult != null)
                            {
                                lock (OAuthCodeCollectionLock)
                                {
                                    OAuthCodeCollection[code] = oAuthAccessTokenResult;
                                }
                            }
                            else
                            {
                                var url = Request.Url.ToString();
                                url = WebTools.BuildUrl(url, "code", "");
                                LogHandler.Info($"code:{code} 已经使用.redirect_uri:{url}");

                                Response.Redirect(url);
                            }
                            //var oAuthAccessTokenResult = OAuthApi.GetAccessToken(BaseClass.AppId, BaseClass.Secret, code);
                            if (oAuthAccessTokenResult.errcode != 0)
                            {
                                Response.Write("您拒绝了授权");
                                LogHandler.Info($"您拒绝了授权,code:{code }");
                            }
                            LogHandler.Info(oAuthAccessTokenResult.access_token + oAuthAccessTokenResult.openid);
                            var oAuthUserInfo = OAuthApi.GetUserInfo(oAuthAccessTokenResult.access_token, oAuthAccessTokenResult.openid);
                            this.openId = oAuthAccessTokenResult.openid;
                            HttpCookie cookie = new HttpCookie(BaseClass.OpenId_Cookie);
                            cookie.Value   = this.openId;
                            cookie.Expires = DateTime.Now.AddDays(1);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            var userModel = new wp_userbll().SaveUserInfo(oAuthUserInfo);
                            LogHandler.Info("oAuthUserInfo:" + JsonHelper.Serialize(oAuthUserInfo) + "|uid:" + userModel);
                            this.uId = userModel;
                            HttpCookie cookieUid = new HttpCookie(BaseClass.Uid_Cookie);
                            cookieUid.Value   = this.uId.ToString();
                            cookieUid.Expires = DateTime.Now.AddDays(1);
                            HttpContext.Current.Response.Cookies.Add(cookieUid);
                            string token = new LoginTokenID(userModel).ToString();
                            WebTools.WriteCookie(WebTools.ych_weixintoken, token, 1);
                            //Response.Redirect("test.aspx"); //Redirect(url);
                            //if (!string.IsNullOrEmpty(code))
                            //{
                            OAuthCodeCollection.Remove(code);
                            //}
                        }
                        catch (Exception ex)
                        {
                            LogHandler.Info($"微信网页授权api信息:{ex.Message}。请求Url:{Request.Url},api参数:url={returnurl},code={code},state={state}");
                            //Response.Write("授权失败");
                        }
                    }
                }
                else
                {
                    openId = "o_7F30X3iijkdt0zsNQrxuGpOL8U";//测试环境账号
                }
            }

            if (this.uId > 0)
            {
                this.currentUser = new Bll.WeChat.wp_userbll().GetUserInfoByUId(this.uId);
                this.openId      = this.currentUser == null ? "" : currentUser.openid;
                LogHandler.Info("currentUser:"******"uid:" + this.uId.ToString());
            }
            if (currentUser == null && !string.IsNullOrEmpty(this.openId))
            {
                this.currentUser = new Bll.WeChat.wp_userbll().GetUserInfoByOpenId(this.openId);
                this.openId      = this.currentUser == null ? "" : currentUser.openid;
                LogHandler.Info("currentUser:"******"openId:" + this.openId.ToString());
            }
            base.OnInit(e);
        }