コード例 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = Request["code"], callBack = Request["callBack"];
            bool stat = false;
            OAuthUserInfo userInfo = null;
            OAuthAccessTokenResult accessToken = null;
            try
            {
                //如果扩展信息不为空尝试拉取信息。

                accessToken = OAuth.GetAccessToken(MPOAuth.OAuthAppID, MPOAuth.OAuthAppSercet, code);
                stat = true;
                if (accessToken.scope == "snsapi_userinfo")
                {
                    userInfo = OAuth.GetUserInfo(accessToken.access_token, accessToken.openid);
                }

            }
            catch (Exception ex)
            {
                Log.Default.Error("获取授权失败!" + ex);
            }
            string key = MPOAuth.WEIXIN_USERINFO;
            Session[key] = new WeiXinUserInfo(stat, userInfo, accessToken);

            Response.Redirect(callBack);
        }
コード例 #2
0
 protected void Page_Init(object sender, EventArgs e)
 {
     string url = null;
     WXAccountDAL dal = new WXAccountDAL();
     if (!OpenID.IsNullOrEmpty())
     {
         OAuthUserInfo userInfo = dal.QuerySingleOpenID(OpenID);
         weiXinUserInfo = new WeiXinUserInfo(true, userInfo, null);
     }
     if (ConfigurationManager.AppSettings["IsDebug"].TryParseBool())
     {
         weiXinUserInfo = new WeiXinUserInfo(true, new OAuthUserInfo() { subscribe = 1, openid = "o3QP1jjbjXDBQZgyPsH5V6pwoubY" }, new OAuthAccessTokenResult() { openid = "o3QP1jjbjXDBQZgyPsH5V6pwoubY" });
     }
     try
     {
         if (weiXinUserInfo == null || weiXinUserInfo.UserInfo == null)
         {
             string backUrl = string.Format("{1}/GetUserInfo.aspx?callBack={0}&appID={2}&appSecret={3}",
                 HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri), ConfigurationManager.AppSettings["ClientDeployDomain"],
                 ConfigurationManager.AppSettings["OAuthAppID"], ConfigurationManager.AppSettings["OAuthAppSercet"]);
             url = OAuth.GetAuthorizeUrl(ConfigurationManager.AppSettings["OAuthAppID"], backUrl, "STATE", OAuthScope.snsapi_userinfo);
             //Log.Default.Debug("url:" + url);
             Response.Redirect(url);
         }
     }
     catch (Exception ex)
     {
         Log.Default.Error(ex);
     }
 }
コード例 #3
0
        protected override void OnPreInit(EventArgs e)
        {
            weiXinUserInfo = HttpContext.Current.Session[MPOAuth.WEIXIN_USERIDETAIL] as WeiXinUserInfo;
            OAuthUserInfo userinfo = null;
            string url = null;
            if (ConfigurationManager.AppSettings["IsDebug"].TryParseBool())
            {

                weiXinUserInfo = new WeiXinUserInfo(true, new OAuthUserInfo() { subscribe = 1 }, new OAuthAccessTokenResult());
            }
            try
            {
                if (weiXinUserInfo == null || weiXinUserInfo.UserInfo == null)
                {
                    string backUrl = string.Format("{1}/GetUserInfo.aspx?callBack={0}&appID={2}&appSecret={3}",
                        HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri), ConfigurationManager.AppSettings["ClientDeployDomain"], ConfigurationManager.AppSettings["OAuthAppID"], ConfigurationManager.AppSettings["OAuthAppSercet"]);
                    url = OAuth.GetAuthorizeUrl(ConfigurationManager.AppSettings["OAuthAppID"], backUrl, "STATE", OAuthScope.snsapi_userinfo);
                }
                else
                {
                    userinfo = weiXinUserInfo.UserInfo;
                }
            }
            catch (Exception ex)
            {
                Log.Default.Error(ex);
            }
            if (userinfo == null)
            {
                HttpContext.Current.Response.Redirect(url);
            }
            base.OnPreInit(e);
        }
コード例 #4
0
 protected override void OnPreInit(EventArgs e)
 {
     userinfo = MPOAuth.CurrentUserInfo;
     if (userinfo == null)
     {
         string requestUrl = HttpContext.Current.Request.Url.AbsoluteUri;
         //如果当前请求为Get请求并且为非Ajax请求
         if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET" && HttpContext.Current.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
         {
             //如果扩展信息为空或者appid,appsercet为空,或者未启用高级直接返回。
             if (string.IsNullOrEmpty(MPOAuth.OAuthAppID) || string.IsNullOrEmpty(MPOAuth.OAuthAppSercet))
             {
                 userinfo = new WeiXinUserInfo(false, null, null);
                 Log.Default.Debug("wx ouath 配置未启用");
             }
             else
             {
                 Log.Default.Debug("wx ouath session为空重新授权,url:" + requestUrl + ";");
                 string backUrl = string.Format("{1}/dooauth.aspx?callBack={0}", HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri), ConfigurationManager.AppSettings["ClientDeployDomain"]);
                 string url = OAuth.GetAuthorizeUrl(MPOAuth.OAuthAppID, backUrl, "STATE", Scope);
                 HttpContext.Current.Response.Redirect(url);
             }
         }
     }
     base.OnPreInit(e);
 }
コード例 #5
0
        //static string appID = "wxa0a406d80dfc308f";
        //static string appSercet = "81e553a85171409db202c70785aed59f";
        /// <summary>
        /// 获取用户信息,如果没有尝试过获取授权会自动获取授权。
        /// 该方法会自动使用微云端进行授权操作。
        /// <para>如果没有尝试获取过获取授权,该方法只对非AJAX GET请求有效</para>
        /// </summary> 
        /// <param name="scope"></param> 
        /// <returns></returns>
        public static WeiXinUserInfo GetUserInfoWithAutoAuth(OAuthScope scope = OAuthScope.snsapi_base)
        {
            string key = WEIXIN_USERINFO;
            HttpContext context = HttpContext.Current;
            WeiXinUserInfo userinfo = HttpContext.Current.Session[key] as WeiXinUserInfo;
            string requestUrl = context.Request.Url.AbsoluteUri;

            //如果当前请求为Get请求并且为非Ajax请求
            if (context.Request.HttpMethod.ToUpper() == "GET" &&
                   context.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
            {

                if ((userinfo == null) || (userinfo.UserInfo == null && scope == OAuthScope.snsapi_userinfo))
                {
                    //如果扩展信息为空或者appid,appsercet为空,或者未启用高级直接返回。
                    if (string.IsNullOrEmpty(_oAuthAppID) ||
                        string.IsNullOrEmpty(_oAuthAppSercet)
                        )
                    {
                        userinfo = new WeiXinUserInfo(false, null, null);
                    }
                    else
                    {
                        Log.Default.Debug("wx ouath session为空重新授权,url:" + requestUrl + ";");
                        string backUrl = string.Format("{1}/dooauth.aspx?callBack={0}", context.Server.UrlEncode(context.Request.Url.AbsoluteUri), ConfigurationManager.AppSettings["ClientDeployDomain"]);
                        string url = OAuth.GetAuthorizeUrl(_oAuthAppID, backUrl, "STATE", scope);
                        HttpContext.Current.Response.Redirect(url);
                    }

                }
            }
            return userinfo;
        }