Example #1
0
        public MobileOAuthUserInfo GetUserInfo_bequiet(ActionExecutingContext filterContext, out string redirectUrl, WXShopInfo settings)
        {
            MobileOAuthUserInfo mobileOAuthUserInfo = null;

            redirectUrl = string.Empty;
            if (!string.IsNullOrEmpty(settings.AppId))
            {
                string item = filterContext.HttpContext.Request["code"];
                if (string.IsNullOrEmpty(item))
                {
                    string str = filterContext.HttpContext.Request.Url.ToString();
                    str         = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", settings.AppId, HttpUtility.UrlEncode(str));
                    redirectUrl = str;
                }
                else
                {
                    string responseResult = GetResponseResult(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", settings.AppId, settings.AppSecret, item));
                    if (responseResult.Contains("access_token"))
                    {
                        JObject             jObjects             = JsonConvert.DeserializeObject(responseResult) as JObject;
                        MobileOAuthUserInfo mobileOAuthUserInfo1 = new MobileOAuthUserInfo()
                        {
                            OpenId        = jObjects["openid"].ToString(),
                            LoginProvider = "ChemCloud.Plugin.OAuth.WeiXin",
                            UnionId       = (jObjects["unionid"] == null || string.IsNullOrWhiteSpace(jObjects["unionid"].ToString()) ? jObjects["openid"].ToString() : jObjects["unionid"].ToString())
                        };
                        mobileOAuthUserInfo = mobileOAuthUserInfo1;
                    }
                }
            }
            return(mobileOAuthUserInfo);
        }
Example #2
0
        public MobileOAuthUserInfo GetUserInfo(ActionExecutingContext filterContext, out string redirectUrl)
        {
            SiteSettingsInfo    siteSettings        = ServiceHelper.Create <ISiteSettingService>().GetSiteSettings();
            MobileOAuthUserInfo mobileOAuthUserInfo = null;

            redirectUrl = string.Empty;
            if (!string.IsNullOrEmpty(siteSettings.WeixinAppId))
            {
                string item = filterContext.HttpContext.Request["code"];
                if (string.IsNullOrEmpty(item))
                {
                    string str = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", siteSettings.WeixinAppId, HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
                    redirectUrl = str;
                }
                else
                {
                    string responseResult = GetResponseResult(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", siteSettings.WeixinAppId, siteSettings.WeixinAppSecret, item));
                    if (responseResult.Contains("access_token"))
                    {
                        JObject  jObjects        = JsonConvert.DeserializeObject(responseResult) as JObject;
                        string[] strArrays       = new string[] { "https://api.weixin.qq.com/sns/userinfo?access_token=", jObjects["access_token"].ToString(), "&openid=", jObjects["openid"].ToString(), "&lang=zh_CN" };
                        string   responseResult1 = GetResponseResult(string.Concat(strArrays));
                        if (responseResult1.Contains("nickname"))
                        {
                            JObject             jObjects1            = JsonConvert.DeserializeObject(responseResult1) as JObject;
                            MobileOAuthUserInfo mobileOAuthUserInfo1 = new MobileOAuthUserInfo()
                            {
                                NickName      = jObjects1["nickname"].ToString(),
                                RealName      = jObjects1["nickname"].ToString(),
                                OpenId        = jObjects1["openid"].ToString(),
                                UnionId       = (jObjects1["unionid"] == null || string.IsNullOrWhiteSpace(jObjects1["unionid"].ToString()) ? jObjects1["openid"].ToString() : jObjects1["unionid"].ToString()),
                                Headimgurl    = jObjects1["headimgurl"].ToString(),
                                LoginProvider = "ChemCloud.Plugin.OAuth.WeiXin"
                            };
                            mobileOAuthUserInfo = mobileOAuthUserInfo1;
                        }
                    }
                }
            }
            return(mobileOAuthUserInfo);
        }
        private bool BindOpenIdToUser(ActionExecutingContext filterContext)
        {
            string       str;
            bool         flag        = true;
            IMobileOAuth weixinOAuth = null;

            if (base.PlatformType == ChemCloud.Core.PlatformType.WeiXin)
            {
                weixinOAuth = new WeixinOAuth();
            }
            string.Format("/m-{0}/Login/Entrance?returnUrl={1}", base.PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
            if (weixinOAuth == null || GetRequestType(filterContext.HttpContext.Request) != ChemCloud.Core.PlatformType.WeiXin)
            {
                flag = false;
            }
            else
            {
                WXShopInfo wXShopInfo = new WXShopInfo();
                string     item       = filterContext.HttpContext.Request["shop"];
                MemberOpenIdInfo.AppIdTypeEnum appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(item))
                {
                    long num = 0;
                    long.TryParse(item, out num);
                    if (num > 0)
                    {
                        wXShopInfo = ServiceHelper.Create <IVShopService>().GetVShopSetting(num);
                    }
                }
                if (string.IsNullOrEmpty(wXShopInfo.AppId) || string.IsNullOrEmpty(wXShopInfo.AppSecret))
                {
                    WXShopInfo wXShopInfo1 = new WXShopInfo()
                    {
                        AppId     = base.CurrentSiteSetting.WeixinAppId,
                        AppSecret = base.CurrentSiteSetting.WeixinAppSecret,
                        Token     = base.CurrentSiteSetting.WeixinToken
                    };
                    wXShopInfo    = wXShopInfo1;
                    appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Payment;
                }
                MobileOAuthUserInfo userInfoBequiet = weixinOAuth.GetUserInfo_bequiet(filterContext, out str, wXShopInfo);
                if (!string.IsNullOrWhiteSpace(str))
                {
                    filterContext.Result = Redirect(str);
                }
                else
                {
                    flag = false;
                    if (userInfoBequiet != null && !string.IsNullOrWhiteSpace(userInfoBequiet.OpenId))
                    {
                        if (appIdTypeEnum == MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {
                            string str1 = SecureHelper.AESEncrypt(userInfoBequiet.OpenId, "Mobile");
                            WebHelper.SetCookie("Himall-User_OpenId", str1);
                        }
                        IMemberService memberService  = ServiceHelper.Create <IMemberService>();
                        UserMemberInfo memberByOpenId = null;
                        if (memberByOpenId == null)
                        {
                            memberByOpenId = memberService.GetMemberByOpenId(userInfoBequiet.LoginProvider, userInfoBequiet.OpenId);
                        }
                        if (memberByOpenId == null)
                        {
                            memberService.BindMember(base.CurrentUser.Id, "ChemCloud.Plugin.OAuth.WeiXin", userInfoBequiet.OpenId, appIdTypeEnum, null, userInfoBequiet.UnionId);
                        }
                        else
                        {
                            string str2 = UserCookieEncryptHelper.Encrypt(memberByOpenId.Id, "Mobile");
                            WebHelper.SetCookie("ChemCloud-User", str2);
                        }
                    }
                }
            }
            return(flag);
        }
        private bool ProcessInvalidUser_NormalRequest(ActionExecutingContext filterContext)
        {
            string       str;
            bool         flag        = true;
            IMobileOAuth weixinOAuth = null;

            if (base.PlatformType == ChemCloud.Core.PlatformType.WeiXin)
            {
                weixinOAuth = new WeixinOAuth();
            }
            string str1 = string.Format("/m-{0}/Login/Entrance?returnUrl={1}", base.PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));

            if (weixinOAuth == null || GetRequestType(filterContext.HttpContext.Request) != ChemCloud.Core.PlatformType.WeiXin)
            {
                filterContext.Result = Redirect(str1);
            }
            else
            {
                WXShopInfo wXShopInfo = new WXShopInfo();
                string     item       = filterContext.HttpContext.Request["shop"];
                MemberOpenIdInfo.AppIdTypeEnum appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(item))
                {
                    long num = 0;
                    long.TryParse(item, out num);
                    if (num > 0)
                    {
                        wXShopInfo = ServiceHelper.Create <IVShopService>().GetVShopSetting(num);
                    }
                }
                if (string.IsNullOrEmpty(wXShopInfo.AppId) || string.IsNullOrEmpty(wXShopInfo.AppSecret))
                {
                    WXShopInfo wXShopInfo1 = new WXShopInfo()
                    {
                        AppId     = base.CurrentSiteSetting.WeixinAppId,
                        AppSecret = base.CurrentSiteSetting.WeixinAppSecret,
                        Token     = base.CurrentSiteSetting.WeixinToken
                    };
                    wXShopInfo    = wXShopInfo1;
                    appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Payment;
                }
                MobileOAuthUserInfo userInfo = weixinOAuth.GetUserInfo(filterContext, out str, wXShopInfo);
                if (!string.IsNullOrWhiteSpace(str))
                {
                    filterContext.Result = Redirect(str);
                }
                else if (userInfo == null || string.IsNullOrWhiteSpace(userInfo.OpenId))
                {
                    filterContext.Result = Redirect(str1);
                }
                else
                {
                    if (appIdTypeEnum == MemberOpenIdInfo.AppIdTypeEnum.Payment)
                    {
                        string str2 = SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                        WebHelper.SetCookie("Himall-User_OpenId", str2);
                    }
                    UserMemberInfo memberByOpenId = null;
                    if (memberByOpenId == null)
                    {
                        memberByOpenId = ServiceHelper.Create <IMemberService>().GetMemberByOpenId(userInfo.LoginProvider, userInfo.OpenId);
                    }
                    if (memberByOpenId == null)
                    {
                        object[] objArray = new object[] { base.PlatformType.ToString(), userInfo.OpenId, "ChemCloud.Plugin.OAuth.WeiXin", HttpUtility.UrlEncode(userInfo.NickName), HttpUtility.UrlEncode(userInfo.RealName), HttpUtility.UrlEncode(userInfo.Headimgurl), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()), appIdTypeEnum, userInfo.UnionId };
                        str1 = string.Format("/m-{0}/Login/Entrance?openId={1}&serviceProvider={2}&nickName={3}&realName={4}&headimgurl={5}&returnUrl={6}&AppidType={7}&unionid={8}", objArray);
                        filterContext.Result = Redirect(str1);
                    }
                    else
                    {
                        string str3 = UserCookieEncryptHelper.Encrypt(memberByOpenId.Id, "Mobile");
                        WebHelper.SetCookie("ChemCloud-User", str3);
                    }
                }
            }
            return(flag);
        }