コード例 #1
0
        /// <summary>
        /// 微信授权回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult WeixinAuth(string code, string state)
        {
            var returnUrl = "";

            if (!string.IsNullOrEmpty(state))
            {
                var data = Encoding.UTF8.GetString(Convert.FromBase64String(state));
                returnUrl = HttpUtility.UrlDecode(data);
            }

            var wxUser = WeiXinAuthor.TryGetUserInfo(code);

            if (wxUser == null)
            {
                return(Content(string.Format("授权失败,请<a href='{0}'>点击此处</>重试", Url.Action("Index", "Auth", new { returnUrl = returnUrl }))));
            }
            Session["WxUser"] = wxUser;
            //缓存并保存微信授权信息
            SaveAuthAccount(wxUser);
            //是否关注,没关注跳关注页面,0没有关注
            if (wxUser.subscribe == 0)
            {
                return(Redirect("/Areas/Wx/Content/zmnbxapp/fouceWeChat.html"));
            }
            var authAccount = aa.Get(wxUser.openid);

            //上一步已经保存微信的信息,所以本次只需要验证是否存在UserId即可
            if (!string.IsNullOrEmpty(authAccount.UserId))
            {
                Session.Remove("User");
                //return Redirect("/Areas/Wx/Content/zmnbxapp/weex.html#/login");
                var data = QueryString(returnUrl);
                if (data["id"] != null)
                {
                    return(Redirect("/Areas/Wx/Content/zmnbxapp/repair.html?id=" + data["id"]));
                }
                else
                {
                    return(Redirect(returnUrl));
                }
            }

            var user = us.Get(authAccount.UserId);

            if (user == null)
            {
                //用户丢失
                Session.Remove("User");
                //return Redirect("/Areas/Wx/Content/zmnbxapp/weex.html#/login");
                var data = QueryString(returnUrl);
                return(Redirect("/Areas/Wx/Content/zmnbxapp/login.html?id=" + data["id"]));
            }

            //跳转对应的页面
            //return Redirect("/Areas/Wx/Content/zmnbxapp/weex.html#/homePage");
            return(Redirect(returnUrl));
        }
コード例 #2
0
        public ActionResult Index(string returnUrl)
        {
            var user = Session["User"] as LZY.BX.Model.User;

            if (user != null)
            {
                if (aa.GetByUserId(user.UserId) == null
                    ||
                    us.Get(user.UserId) == null)
                {
                    Session.Remove("User");
                    Session.Remove("WxUser");
                }
            }

            if (Session["User"] != null)
            {
                //跳转到登录后的首页
                //return Redirect("/Areas/Wx/Content/zmnbxapp/weex.html#/homePage");
                return(Redirect(returnUrl));
            }

            //授权登录
            var state = "";

            if (!string.IsNullOrEmpty(returnUrl))
            {
                var data = Convert.ToBase64String(Encoding.UTF8.GetBytes(returnUrl));
                state = HttpUtility.UrlEncode(data);
            }
            ////如果数据库找到授权信息
            if (Session["WxUser"] != null)
            {
                //静默授权
                return(Redirect(WeiXinAuthor.OauthUrl(HttpUtility.UrlEncode(string.Concat(Config.DoMain, Url.Action("WeixinAuth", "Auth"))), "snsapi_base", state)));
            }
            //弹框授权
            return(Redirect(WeiXinAuthor.OauthUrl(HttpUtility.UrlEncode(string.Concat(Config.DoMain, Url.Action("WeixinAuth", "Auth"))), "snsapi_userinfo", state)));
        }