Пример #1
0
        /// <summary>
        /// OAuthScope.snsapi_userinfo方式回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="returnUrl">用户最初尝试进入的页面</param>
        /// <returns></returns>
        public async Task <ActionResult> BaseCallback(string code, string returnUrl)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(Content("您拒绝了授权!"));
            }

            try
            {
                var appKey      = AccessTokenContainer.BuildingKey(_workWeixinSetting);
                var accessToken = await AccessTokenContainer.GetTokenAsync(_corpId, _corpSecret);

                //获取用户信息 测试链接:https://open.work.weixin.qq.com/wwopen/devtool/interface?doc_id=10019
                var oauthResult = await OAuth2Api.GetUserIdAsync(accessToken, code);

                var             userId = oauthResult.UserId;
                GetMemberResult result = await MailListApi.GetMemberAsync(appKey, userId);

                if (result.errcode != ReturnCode_Work.请求成功)
                {
                    return(Content("错误:" + result.errmsg));
                }

                ViewData["returnUrl"] = returnUrl;

                /* 注意:
                 * 实际适用场景,此处应该跳转到 returnUrl,不要停留在 Callback页面上。
                 * 因为当用户刷新此页面 URL 时,实际 code 等参数已失效,用户会受到错误信息。
                 */
                return(View(result));
            }
            catch (Exception ex)
            {
                return(Content("错误:" + ex.Message));
            }
        }