/// <summary>
        /// 增加返回信息
        /// </summary>
        /// <param name="content"></param>
        public void Add(CallbackContentInfo content)
        {
            ArgumentHelper.AssertIsTrue(content != null, "content is null");
            ArgumentHelper.AssertIsTrue(content.TenantId >= 0, "AppAccountInfo.TenantId is less than 0");
            ArgumentHelper.AssertNotNullOrEmpty(content.BatchId, "content.BatchId is null or empty");
            ArgumentHelper.AssertNotNullOrEmpty(content.AppAccountPublic, "content.AppAccountPublic is null or empty");
            ArgumentHelper.AssertNotNullOrEmpty(content.Content, "content.Content is null or empty");

            CallbackContentDao.Insert(content);
        }
Example #2
0
 /// <summary>
 /// 数据映射
 /// </summary>
 /// <param name="record">记录</param>
 /// <param name="info">实体</param>
 private static void BuildInfo(IRecord record, CallbackContentInfo info)
 {
     info.Id                = record.Get <int>("Id");
     info.BatchId           = record.Get <Guid>("BatchId").ToString();
     info.TenantId          = record.Get <int>("TenantId");
     info.AppAccountPublic  = record.Get <string>("AppAccountPublic");
     info.AppAccountPrivate = record.Get <string>("AppAccountPrivate");
     info.Content           = record.Get <string>("Content");
     info.State             = (CallbackContentState)record.Get <short>("State");
 }
        /// <summary>
        /// 获取绑定批次Id
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="redirectUrl"></param>
        /// <returns></returns>
        public string GetBindBatchId(int tenantId, string appAccountId, int type, string redirectUrl)
        {
            var            appAccountPublic   = new AppAccountInfo();
            var            appAccountPublicId = string.Empty;
            AppAccountInfo appAccountPrivate  = null;

            //根据类型初始化信息
            if (string.IsNullOrWhiteSpace(appAccountId))
            {
                //公共账户方式
                appAccountPublic   = ProviderGateway.AppAccountProvider.GetPublicByType(type);
                appAccountPublicId = appAccountPublic.AppAccountId;
            }
            else
            {
                //私有账户方式
                appAccountPrivate = ProviderGateway.AppAccountProvider.Get(appAccountId);

                if (tenantId != 0 && tenantId != appAccountPrivate.TenantId)
                {
                    return(string.Empty);
                }

                tenantId = appAccountPrivate.TenantId;
                switch (appAccountPrivate.Type)
                {
                case 11:
                case 12:
                    appAccountPublic   = ProviderGateway.AppAccountProvider.GetPublicByType(appAccountPrivate.Type);
                    appAccountPublicId = appAccountPublic.AppAccountId;
                    break;

                default:
                    appAccountPublicId = appAccountPrivate.AppAccountId;
                    break;
                }
            }

            //记录回调信息
            var batchId         = Guid.NewGuid().ToString();
            var callbackContent = new CallbackContentInfo
            {
                BatchId           = batchId,
                TenantId          = tenantId,
                AppAccountPublic  = appAccountPublicId,
                AppAccountPrivate = appAccountPrivate == null ? null : appAccountPrivate.AppAccountId,
                Content           = redirectUrl,
                State             = CallbackContentState.RegisterBind
            };

            ProviderGateway.CallbackContentProvider.Add(callbackContent);

            return(batchId);
        }
        internal override string GetRedirectUrl(CallbackContentInfo contentInfo, string code, string state)
        {
            var appAccountPvivate = ProviderGateway.AppAccountProvider.Get(contentInfo.AppAccountPrivate);

            GetOpenId(appAccountPvivate, code);
            SetNextState(contentInfo.BatchId, CallbackContentState.Finish);
            //跳转获取私有身份
            var rd = new Random();
            var r  = rd.Next();

            //返回身份
            return(UrlHelper.AddParameter(contentInfo.Content, "r", r.ToString()));
        }
        public void TestAdd()
        {
            var content = new CallbackContentInfo();

            content.BatchId           = Guid.NewGuid().ToString();
            content.TenantId          = 0;
            content.AppAccountPublic  = "AppAccountPublic";
            content.AppAccountPrivate = "AppAccountPrivate";
            content.Content           = "Content";
            content.State             = CallbackContentState.Public;

            ProviderGateway.CallbackContentProvider.Add(content);
        }
Example #6
0
        /// <summary>
        /// 增加返回信息
        /// </summary>
        /// <param name="content"></param>
        public static void Insert(CallbackContentInfo content)
        {
            var db = Database.GetDatabase(DatabaseName.AppConnect);

            SafeProcedure.ExecuteNonQuery(db, "dbo.CallbackContent_Insert", parameterMapper =>
            {
                parameterMapper.AddWithValue("@BatchId", new Guid(content.BatchId));
                parameterMapper.AddWithValue("@TenantId", content.TenantId);
                parameterMapper.AddWithValue("@AppAccountPublic", content.AppAccountPublic);
                parameterMapper.AddWithValue("@AppAccountPrivate", content.AppAccountPrivate);
                parameterMapper.AddWithValue("@Content", content.Content);
                parameterMapper.AddWithValue("@State", (short)content.State);
            });
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="returnContentInfo"></param>
        /// <returns></returns>
        internal static CallbackStateBase GetIntance(CallbackContentInfo returnContentInfo)
        {
            switch (returnContentInfo.State)
            {
            case CallbackContentState.Public:
                return(CallbackPublicState.Intance);

            case CallbackContentState.Private:
                return(CallbackPrivateState.Intance);

            case CallbackContentState.PrivateAndPublic:
                return(CallbackPrivateAndPublicState.Intance);

            default:
                throw new ArgumentException("回调状态无效:state=" + (int)returnContentInfo.State);
            }
        }
Example #8
0
        internal override string GetRedirectUrl(CallbackContentInfo contentInfo, string code, string state)
        {
            var appAccountPublic = ProviderGateway.AppAccountProvider.Get(contentInfo.AppAccountPublic);

            GetOpenId(appAccountPublic, code);

            var appAccountPrivate = ProviderGateway.AppAccountProvider.Get(contentInfo.AppAccountPrivate);
            var extend            = new Dictionary <string, string>
            {
                { TemplateConst.ExtendState, state },
                { TemplateConst.ExtendBatch, contentInfo.BatchId }
            };
            var requestTemplate = new DefaultApiTemplate(appAccountPrivate, TemplateConst.AuthorizeUrl, extend);

            SetNextState(contentInfo.BatchId, CallbackContentState.Private);

            //跳转获取私有身份
            return(requestTemplate.GetRequestUrl());
        }
 internal abstract string GetRedirectUrl(CallbackContentInfo contentInfo, string code, string state);
        /// <summary>
        /// 获取开放平台授权URL
        /// </summary>
        /// <param name="appAccountId"></param>
        /// <param name="redirectUrl"></param>
        /// <param name="type">公共服务号类型。12:微信服务号,21:钉钉</param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public string GetAuthorizeUrl(string appAccountId, string redirectUrl, int type, UserCookie cookie, string loginType = null)
        {
            //Log 信息
            //  AppConnectLogHelper.ErrorFormat("进入GetAuthorizeUrl方法!appAccountId:{0},redirectUrl:{1},type:{2}", appAccountId, redirectUrl, type);
            ArgumentHelper.AssertIsTrue(!string.IsNullOrWhiteSpace(redirectUrl), "redirectUrl is null or empty");

            AppAccountInfo       appAccountPublic;
            AppAccountInfo       appAccountPrivate;
            AppAccountInfo       appAccountAuthorize;
            string               appAccountPublicId;
            string               appAccountPrivateId = null;
            CallbackContentState callbackState;
            var tenantId = 0;

            //根据类型初始化信息
            if (string.IsNullOrWhiteSpace(appAccountId))
            {
                //公共账户方式
                AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->公共账户方式!");
                appAccountPublic = ProviderGateway.AppAccountProvider.GetPublicByType(type);

                if (!string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPublic.AppId)))
                {
                    return(null);
                }

                appAccountPublicId  = appAccountPublic.AppAccountId;
                callbackState       = CallbackContentState.Public;
                appAccountAuthorize = appAccountPublic;
            }
            else
            {
                //私有账户类型
                AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!");
                appAccountPrivate = ProviderGateway.AppAccountProvider.Get(appAccountId);
                if (appAccountPrivate == null)
                {
                    AppConnectLogHelper.Error("AppAccountProvider-GetByAppAccountId 为 null");
                    return(null);
                }
                appAccountPrivateId = appAccountPrivate.AppAccountId;
                tenantId            = appAccountPrivate.TenantId;

                switch (appAccountPrivate.Type)
                {
                case 11:
                case 12:
                    AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case11,12");
                    appAccountPublic   = ProviderGateway.AppAccountProvider.GetPublicByType(appAccountPrivate.Type);
                    appAccountPublicId = appAccountPublic.AppAccountId;

                    if (!string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPublic.AppId)) && !string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPrivate.AppId)))
                    {
                        return(null);
                    }

                    if (!string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPublic.AppId)))
                    {
                        callbackState       = CallbackContentState.Private;
                        appAccountAuthorize = appAccountPrivate;
                    }
                    else if (!string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPrivate.AppId)))
                    {
                        callbackState       = CallbackContentState.Public;
                        appAccountAuthorize = appAccountPublic;
                    }
                    else
                    {
                        callbackState       = CallbackContentState.PrivateAndPublic;
                        appAccountAuthorize = appAccountPublic;
                    }
                    break;

                default:
                    AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->CaseDefault");
                    if (string.IsNullOrWhiteSpace(CookieHelper.GetOpenId(cookie, appAccountPrivate.AppId)))
                    {
                        callbackState       = CallbackContentState.Private;
                        appAccountAuthorize = appAccountPrivate;
                        appAccountPublicId  = appAccountPrivate.AppAccountId;
                    }
                    else
                    {
                        AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->CaseDefault->Cookie有值返回Null");
                        return(null);
                    }
                    break;
                }
            }
            AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case->记录回调信息");
            //记录回调信息
            var batchId         = Guid.NewGuid().ToString();
            var callbackContent = new CallbackContentInfo
            {
                BatchId           = batchId,
                TenantId          = tenantId,
                AppAccountPublic  = appAccountPublicId,
                AppAccountPrivate = appAccountPrivateId,
                Content           = redirectUrl,
                State             = callbackState
            };

            ProviderGateway.CallbackContentProvider.Add(callbackContent);

            var state = Guid.NewGuid().ToString("N");

            AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case->记录回调信息->记录Cookie");
            try
            {
                AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case->记录回调信息->记录Cookie前->State:" + state);

                CookieHelper.SetState(state);
                var getCookieState = CookieHelper.GetState();
                AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case->记录回调信息->记录Cookie后获取->State:" + getCookieState);
            }
            catch (System.Exception ex)
            {
                AppConnectLogHelper.Debug("SetState加日志发生了错误!!!!!");
            }

            //生成授权Url
            var extend = new Dictionary <string, string>
            {
                { TemplateConst.ExtendState, state },
                { TemplateConst.ExtendBatch, batchId }
            };

            string url = null;

            //钉钉的PC登录
            if (!string.IsNullOrEmpty(loginType) && loginType == "0")
            {
                var requestTemplate = new DefaultApiTemplate(appAccountAuthorize, TemplateConst.AuthorizePCUrl, extend);
                url = requestTemplate.GetRequestUrl();
            }
            else
            {
                var requestTemplate = new DefaultApiTemplate(appAccountAuthorize, TemplateConst.AuthorizeUrl, extend);
                url = requestTemplate.GetRequestUrl();
            }
            AppConnectLogHelper.Debug("进入GetAuthorizeUrl方法->私有账户方式!->Case->记录回调信息->记录Cookie后获取->State->Url" + url);
            return(url);
        }
        /// <summary>
        /// 校验用户身份,并返回绑定Url
        /// </summary>
        /// <param name="appAccountId"></param>
        /// <param name="redirectUrl"></param>
        /// <param name="type"></param>
        /// <param name="cookie"></param>
        /// <returns>
        /// null:用户身份存在且状态正常
        /// 非null:返回绑定Url,直接跳转
        /// </returns>
        public string GetIdentityUrl(string appAccountId, string redirectUrl, int type, UserCookie cookie, CallbackContentState state = CallbackContentState.Bind)
        {
            ArgumentHelper.AssertIsTrue(!string.IsNullOrWhiteSpace(redirectUrl), "redirectUrl is null or empty");

            AppAccountInfo     appAccountPublic;
            AppAccountInfo     appAccountPrivate = null;
            var                tenantId          = 0;
            AppUserAccountInfo appUserAccountMaster;
            AppUserAccountInfo appUserAccount;
            string             appAccountPublicId;

            //根据类型初始化信息
            if (string.IsNullOrWhiteSpace(appAccountId))
            {
                //公共账户方式
                appAccountPublic     = ProviderGateway.AppAccountProvider.GetPublicByType(type);
                appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPublic.AppId, cookie.OpenIds[appAccountPublic.AppId]);
                appAccountPublicId   = appAccountPublic.AppAccountId;

                if (appUserAccountMaster != null && appUserAccountMaster.State == AppUserAccountState.Activated)
                {
                    cookie.AccountAppId  = appAccountPublic.AppId;
                    cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];
                    return(null);
                }
            }
            else
            {
                //私有账户方式
                appAccountPrivate = ProviderGateway.AppAccountProvider.Get(appAccountId);
                tenantId          = appAccountPrivate.TenantId;

                switch (appAccountPrivate.Type)
                {
                case 11:
                case 12:
                    appAccountPublic     = ProviderGateway.AppAccountProvider.GetPublicByType(appAccountPrivate.Type);
                    appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPublic.AppId, cookie.OpenIds[appAccountPublic.AppId]);
                    appUserAccount       = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPrivate.AppId, cookie.OpenIds[appAccountPrivate.AppId]);
                    appAccountPublicId   = appAccountPublic.AppAccountId;

                    if (appUserAccountMaster == null)
                    {
                        if (appUserAccount != null)
                        {
                            //如果与其他账户冲突
                            var appUserAccoutTemp = ProviderGateway.AppUserAccountProvider.GetByUserId(appUserAccount.TenantId, appUserAccount.UserId, appAccountPublic.AppId);
                            if (appUserAccoutTemp == null)
                            {
                                //补充主账户
                                var appUserAccountMasterNew = new AppUserAccountInfo();
                                appUserAccountMasterNew.AppId           = appAccountPublic.AppId;
                                appUserAccountMasterNew.OpenId          = cookie.OpenIds[appAccountPublic.AppId];
                                appUserAccountMasterNew.TenantId        = appUserAccount.TenantId;
                                appUserAccountMasterNew.UserId          = appUserAccount.UserId;
                                appUserAccountMasterNew.BeisenAccount   = BeisenUserProxy.GetUserEmail(appUserAccount.UserId);
                                appUserAccountMasterNew.Type            = AppUserAccountType.Email;
                                appUserAccountMasterNew.State           = AppUserAccountState.Activated;
                                appUserAccountMasterNew.MasterAccountId = 0;
                                var appUserAccountMasterNewId = ProviderGateway.AppUserAccountProvider.AddOrUpdate(appUserAccountMasterNew.TenantId, appUserAccountMasterNew);
                                ProviderGateway.AppUserAccountProvider.UpdateMasterAccountId(appUserAccount.Id, appUserAccountMasterNewId);

                                cookie.AccountAppId  = appAccountPublic.AppId;
                                cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];

                                return(null);
                            }
                        }
                    }
                    else if (appUserAccountMaster.State == AppUserAccountState.Activated)
                    {
                        if (appUserAccount == null)
                        {
                            //补充账户信息
                            var appUserAccountNew = new AppUserAccountInfo();
                            appUserAccountNew.AppId           = appAccountPrivate.AppId;
                            appUserAccountNew.OpenId          = cookie.OpenIds[appAccountPrivate.AppId];
                            appUserAccountNew.TenantId        = appUserAccountMaster.TenantId;
                            appUserAccountNew.UserId          = appUserAccountMaster.UserId;
                            appUserAccountNew.BeisenAccount   = appUserAccountMaster.BeisenAccount;
                            appUserAccountNew.Type            = appUserAccountMaster.Type;
                            appUserAccountNew.State           = appUserAccountMaster.State;
                            appUserAccountNew.MasterAccountId = appUserAccountMaster.Id;
                            ProviderGateway.AppUserAccountProvider.AddOrUpdate(appUserAccountNew.TenantId, appUserAccountNew);
                        }
                        else if (appUserAccount.MasterAccountId != appUserAccountMaster.Id)
                        {
                            //更新MasterId
                            ProviderGateway.AppUserAccountProvider.UpdateMasterAccountId(appUserAccount.Id, appUserAccountMaster.Id);
                        }
                        cookie.AccountAppId  = appAccountPublic.AppId;
                        cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];
                        return(null);
                    }
                    break;

                default:
                    appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPrivate.AppId, cookie.OpenIds[appAccountPrivate.AppId]);
                    appAccountPublicId   = appAccountPrivate.AppAccountId;
                    if (appUserAccountMaster != null && appUserAccountMaster.State == AppUserAccountState.Activated)
                    {
                        cookie.AccountAppId  = appAccountPrivate.AppId;
                        cookie.AccountOpenId = cookie.OpenIds[appAccountPrivate.AppId];
                        return(null);
                    }
                    break;
                }
            }
            //记录回调信息
            var batchId         = Guid.NewGuid().ToString();
            var callbackContent = new CallbackContentInfo
            {
                BatchId           = batchId,
                TenantId          = tenantId,
                AppAccountPublic  = appAccountPublicId,
                AppAccountPrivate = appAccountPrivate == null ? null : appAccountPrivate.AppAccountId,
                Content           = redirectUrl,
                State             = state
            };

            ProviderGateway.CallbackContentProvider.Add(callbackContent);

            if (state == CallbackContentState.Bind)
            {
                return(UrlHelper.AddParameter(HostConst.UserBind, "batch", batchId));
            }
            else
            {
                var parameter = redirectUrl.Split('?');
                var query     = parameter.Length > 0 ? string.Format("{0}&batch={1}", parameter[1], batchId) : string.Empty;
                return(UrlHelper.AddQuery(HostConst.RegisterBind, query));
            }
        }