c# 微信開發 《受權頁面》

//受權頁面
public partial class wxProcess : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string reurl = "";
            //傳遞參數,獲取用戶信息後,可跳轉到本身定義的頁面,想怎麼處理就怎麼處理
            if (Request.QueryString["reurl"] != null && Request.QueryString["reurl"] != "")
            {
                reurl = Request.QueryString["reurl"].ToString();
            }
            else
            {
                
            }

            string code = "";
            string url = "";
            string appid = ConfigurationManager.AppSettings["WeixinAppid"].TryToString();
            //彈出受權頁面(如在不彈出受權頁面基礎下未得到openid,彈出受權頁面,提示用戶受權)
            if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
            {
                url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid
                                   + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
                Response.Redirect(url);
            }
            else
            {
                url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                                + appid + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
                //不彈出受權頁面
                Response.Redirect(url);
            }
        }
    }
}


///需跳轉頁面
public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = "";
            if (Request.QueryString["code"] != null && Request.QueryString["code"] != "")
            {
                //獲取微信回傳的code
                code = Request.QueryString["code"].ToString();
                string openid = WeiXinCommom.Get_token(code);  //獲取token
                if (!string.IsNullOrWhiteSpace(openid)) {
                    WeiXinService WeiXinService = new WeiXinService();
                    Entity UserInfo= WeixinDA.GetShowUserWeiXinInfo(openid); //獲取用戶信息
                    LogHelper.WriteFileLog("UserInfo", UserInfo.IsNull.ToString());
                    bool SaveUser = true;
                    if (UserInfo.IsNull) {  //若是不存在用戶信息,用戶已經關注了公衆號,可是未註冊到用戶數據,從新註冊
                        int CareUsCode = 11000;//關注公衆號贈送積分的codeid
                        Entity CareUsData = WeixinDA.GetCategoryCodesByCodeID(CareUsCode);
                        int CareUsStatus = CareUsData.GetValue("Status").TryInt32();
                        SaveUser= WeiXinService.SaveUserInfo(openid); //返回fasle,用戶未關注公衆號
                        if (SaveUser) {
                            if (CareUsStatus == (int)BaseEnum.OnOff.On)//積分規則打開
                            {
                                decimal Integration = CareUsData.GetValue("Value").TryDecimal();
                                WeiXinService.AddOpenIDIntegration(openid, (int)BaseEnum.IntegrationType.CareUs, Integration);//第一次關注公衆號贈送的積分
                            }
                        }
                    }
                    if (SaveUser)
                    {
                        string WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "orderList", "<script>UserProfile.GetWeixinCode('" + WeiXinInfoString + "');</script>");
                    }
                    else {
                        string WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "orderList", "<script>UserProfile.GetWeixinCode('');</script>");
                    }
                }
            }
        }
    }
//根據appid,secret,code獲取微信openid、access token信息
        public static string Get_token(string Code)
        {
            //獲取微信回傳的openid、access token
            string WeixinData = WeiXinCommom.GetData("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + Code + "&grant_type=authorization_code");
            string openid = string.IsNullOrWhiteSpace(WeixinData) ? string.Empty : JObject.Parse(WeixinData)["openid"].TryString();
            return openid;
        }

  

訪問 HTTP://baidu.com.受權頁面?需跳轉頁面
相關文章
相關標籤/搜索