Ejemplo n.º 1
0
        /// <summary>
        /// 用于接收取消授权通知、授权成功通知、授权更新通知
        /// </summary>
        /// <param name="auth_code"></param>
        /// <param name="expires_in"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public ActionResult OAuthCallback(string auth_code, int expires_in, string appId)
        {
            try
            {
                //获取公众号授权结果
                QueryAuthResult queryAuthResult;
                try
                {
                    queryAuthResult = ComponentContainer.GetQueryAuthResult(component_AppId, auth_code);
                }
                catch (Exception ex)
                {
                    throw new Exception("QueryAuthResult:" + ex.Message);
                }

                var authorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(component_AppId,
                                                                                       queryAuthResult.authorization_info.authorizer_appid);

                ViewData["AuthorizationInfo"] = queryAuthResult.authorization_info;
                ViewData["AuthorizerInfo"]    = authorizerInfoResult.authorizer_info;

                //sdk已经本地存储授权信息,是否要将授权信息存储到数据库?

                return(View());
            }
            catch (ErrorJsonResultException ex)
            {
                return(Content(ex.Message));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 授权的公众号appid
        /// </summary>
        /// <param name="authorizerAppId"></param>
        /// <returns></returns>
        private GetAuthorizerInfoResult GetAuthorizerInfoResult(string authorizerAppId)
        {
            var getAuthorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(component_AppId, authorizerAppId);

            getAuthorizerInfoResult.authorization_info.authorizer_appid = authorizerAppId;
            return(getAuthorizerInfoResult);
        }
Ejemplo n.º 3
0
        public APIResult UpdateAuthorizerInfo([FromBody] ShopIdArgsModel args)
        {
            if (!args.ShopId.HasValue)
            {
                throw new ArgumentNullException("ShopId");
            }
            CheckShopActor(args.ShopId.Value, ShopActorType.超级管理员);

            var model = db.Query <ShopWechatOpenAuthorizer>()
                        .Where(m => !m.IsDel)
                        .Where(m => m.ShopId == args.ShopId.Value)
                        .Select(m => m.WechatOpenAuthorizer)
                        .FirstOrDefault();

            if (model == null)
            {
                throw new Exception("指定的纪录不存在");
            }

            var authorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(wechatOpenOptions.AppId,
                                                                                   model.AuthorizerAppId);

            if (authorizerInfoResult.errcode != Senparc.Weixin.ReturnCode.请求成功)
            {
                throw new Exception("请求失败:" + authorizerInfoResult.errcode);
            }

            model.AuthorizerNickname = authorizerInfoResult.authorizer_info.nick_name;
            model.AuthorizerUsername = authorizerInfoResult.authorizer_info.user_name;
            db.SaveChanges();

            return(Success());
        }
Ejemplo n.º 4
0
        public ActionResult GetAuthorizerInfoResultPage(string authorizerAppId)
        {
            WeixinTrace.SendCustomLog("查询授权信息json", authorizerAppId);//记录到日志中
            var getAuthorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(component_AppId, authorizerAppId);

            getAuthorizerInfoResult.authorization_info.authorizer_appid = authorizerAppId;
            return(Json(getAuthorizerInfoResult, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 5
0
        //private static string ComponentAccessToken = null;//需要授权获取,腾讯服务器会主动推送


        #region 开放平台入口及回调

        /// <summary>
        /// OAuthScope.snsapi_userinfo方式回调
        /// </summary>
        /// <param name="auth_code"></param>
        /// <param name="expires_in"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public ActionResult OpenOAuthCallback(string auth_code, int expires_in, string appId)
        {
            try
            {
                #region 直接调用API

                //string openTicket = OpenTicketHelper.GetOpenTicket(component_AppId);
                //var component_access_token = Open.ComponentAPIs.ComponentApi.GetComponentAccessToken(component_AppId, component_Secret, openTicket).component_access_token;
                //ComponentAccessToken = component_access_token;
                //var oauthResult = Open.ComponentAPIs.ComponentApi.QueryAuth(component_access_token, component_AppId, auth_code);

                ////TODO:储存oauthResult.authorization_info
                //var authInfoResult = Open.ComponentAPIs.ComponentApi.GetAuthorizerInfo(component_access_token, component_AppId,
                //     oauthResult.authorization_info.authorizer_appid);

                #endregion

                #region 使用ComponentContainer

                //获取OAuth授权结果
                QueryAuthResult queryAuthResult;
                try
                {
                    queryAuthResult = ComponentContainer.GetQueryAuthResult(component_AppId, auth_code);
                }
                catch (Exception ex)
                {
                    throw new Exception("QueryAuthResult:" + ex.Message);
                }
                #endregion

                var authorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(component_AppId,
                                                                                       queryAuthResult.authorization_info.authorizer_appid);

                ViewData["QueryAuthorizationInfo"]  = queryAuthResult.authorization_info;
                ViewData["GetAuthorizerInfoResult"] = authorizerInfoResult.authorizer_info;
                ViewData["ClientId"] = Request.QueryString["clientId"];
                var clientId = Request.QueryString["clientId"];

                //保存到微信信息表
                SetWechatApp(queryAuthResult, authorizerInfoResult, clientId);

                return(View());
            }
            catch (ErrorJsonResultException ex)
            {
                return(Content(ex.Message));
            }
        }
Ejemplo n.º 6
0
        public ActionResult RefreshAuthorizerAccessToken(string authorizerId)
        {
            var componentAccessToken = ComponentContainer.GetComponentAccessToken(component_AppId);
            var authorizationInfo    = AuthorizerContainer.GetAuthorizationInfo(component_AppId, authorizerId);

            if (authorizationInfo == null)
            {
                return(Content("授权信息读取失败!"));
            }

            var refreshToken = authorizationInfo.authorizer_refresh_token;
            var result       = AuthorizerContainer.RefreshAuthorizerToken(componentAccessToken, component_AppId, authorizerId,
                                                                          refreshToken);

            return(Json(result));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public static string GetAuthorizerAccessToken(this ShopDbContext db, string appId, int shopId)
        {
            var model = db.Query <ShopWechatOpenAuthorizer>()
                        .Where(m => !m.IsDel)
                        .Where(m => m.ShopId == shopId)
                        .Select(m => new
            {
                m.WechatOpenAuthorizer.AuthorizerAppId,
                m.WechatOpenAuthorizer.AuthorizerAccessToken,
                m.WechatOpenAuthorizer.ExpiresTime,
            })
                        .FirstOrDefault();

            if (model == null)
            {
                throw new Exception("指定的授权纪录不存在");
            }
            var authorizerAccessToken = AuthorizerContainer.TryGetAuthorizerAccessToken(appId, model.AuthorizerAppId);

            return(authorizerAccessToken);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 【异步方法】获取QueryAuthResult(此方法每次都会发出请求,不缓存)
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="authorizationCode"></param>
        /// <param name="updateToAuthorizerContanier">是否将Authorization更新到AuthorizerContanier</param>
        /// <param name="getNewToken"></param>
        /// <returns></returns>
        /// <exception cref="WeixinOpenException"></exception>
        public static async Task<QueryAuthResult> GetQueryAuthResultAsync(string componentAppId, string authorizationCode, bool updateToAuthorizerContanier = true, bool getNewToken = false)
        {
            if (!CheckRegistered(componentAppId))
            {
                throw new WeixinOpenException(UN_REGISTER_ALERT);
            }

            var componentBag = TryGetItem(componentAppId);
            using (Cache.BeginCacheLock(LockResourceName + ".GetQueryAuthResult", componentAppId))//同步锁
            {
                var accessToken = TryGetComponentAccessToken(componentAppId, componentBag.ComponentAppSecret, null, getNewToken);
                var queryAuthResult = await ComponentApi.QueryAuthAsync(accessToken, componentAppId, authorizationCode);

                if (updateToAuthorizerContanier)
                {
                    //更新到AuthorizerContainer
                    AuthorizerContainer.TryUpdateAuthorizationInfo(componentAppId, queryAuthResult.authorization_info.authorizer_appid, queryAuthResult.authorization_info);
                }

                return queryAuthResult;
            }
        }
Ejemplo n.º 9
0
        public ActionResult GetAuthorizerInfoResult(string authorizerId)
        {
            var getAuthorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(component_AppId, authorizerId);

            return(Json(getAuthorizerInfoResult));
        }
Ejemplo n.º 10
0
        public ActionResult GetAuthorizerInfoResult(string authorizerId)
        {
            var getAuthorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(appId, authorizerId);

            return(Json(getAuthorizerInfoResult, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 11
0
        string GetAuthorizerAccessToken(string authorizerAppId)
        {
            var authorizerAccessToken = AuthorizerContainer.TryGetAuthorizerAccessToken(wechatOpenOptions.AppId, authorizerAppId);

            return(authorizerAccessToken);
        }