Example #1
0
        /// <summary>
        /// 请求用户授权认证
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public static string RequestUserAuthPtl(this WxRequestAuthSetting setting)
        {
            if (String.IsNullOrEmpty(setting.AppId) || String.IsNullOrEmpty(setting.RedirectUri) ||
                setting.ScopeStorage == null || !setting.ScopeStorage.Any())
            {
                throw new ArgumentException(@"传递的设置有问题");
            }

            //TODO 后续版本中支持从date 获取Session
            var          urlEncoded = HttpUtility.UrlEncode(setting.RedirectUri);
            const string format     = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}#wechat_redirect";
            var          url        = string.Format(format, setting.AppId, urlEncoded, setting.ResponseType,
                                                    String.Join(",", setting.ScopeStorage), "wx");

            return(url);
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建一个会话生成器,该生成器用来生成和认证服务器的会话
            WxAuthenticationSessionBuilder sessionBuilder = new WxAuthenticationSessionBuilder();

            //传递给认证服务器的协议参数,具体http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
            var setting = new WxRequestAuthSetting("http://weixinchat.ngrok.com/Response.aspx", new List <string>()
            {
                WxScope.snsapi_userinfo.ToString()
            })
            {
                AppId = "AppId"
            };

            //因为该事例采用server-side方式来认证,所以直接引导用户到提供商认证服务器的页面处理器上去
            var wxAuthenticationSession = sessionBuilder.Build(setting);

            if (wxAuthenticationSession != null)
            {
                wxAuthenticationSession.Direct(Response);
            }
        }