コード例 #1
0
        // GET: OAuth

        /// <summary>
        /// 授权回调入口
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Index(string code)
        {
            RPoney.Log.LoggerManager.Debug(GetType().Name, $"授权回调处理,code:{code}");
            try
            {
                var getToken = ApiCommon.GetToken(AppConfigBll.AppConfig.AppKey, AppConfigBll.AppConfig.AppSecrect, AppConfigBll.AppConfig.AppRediretUrl, code);
                if (null == getToken)
                {
                    return(View("_Error"));
                }
                RPoney.Log.LoggerManager.Debug(GetType().Name, $"查找商家token:{getToken.SerializeToJSON()}");
                var productUserToken = new T_ProductUserTokenEntity
                {
                    AliId               = getToken.AliId,
                    MemberId            = getToken.MemberId,
                    ResourceOwner       = getToken.ResourceOwner,
                    AccessToken         = getToken.AccessToken,
                    RefreshToken        = getToken.RefreshToken,
                    ExpiresIn           = getToken.ExpiresIn.CInt(0, false),
                    RefreshTokenTimeout = getToken.RefreshTokenTimeout.GetDateTimeFromUtc(DateTime.MinValue),
                    UpdateTime          = DateTime.Now
                };
                RPoney.Log.LoggerManager.Debug(GetType().Name, $"进入授权回调处理,productUserToken:{productUserToken.SerializeToJSON()}");
                if (new T_ProductUserTokenBll().Save(productUserToken))
                {
                    var productUserBll = new ProductUserBll();
                    var user           = productUserBll.GetProductUser(getToken.ResourceOwner);
                    if (null == user)
                    {
                        productUserBll.Add(new ProductUserEntity()
                        {
                            ResourceOwner = getToken.ResourceOwner
                        });
                        user = productUserBll.GetProductUser(getToken.ResourceOwner);
                    }
                    if (null != user)
                    {
                        TicketStorageFactory.InstanceTicketStorage <ProductUserViewModel>().SetTicket(user);
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                RPoney.Log.LoggerManager.Debug(GetType().Name, $"保存授权信息失败,code:{code}");
                return(View("_Error"));
            }
            catch (Exception ex)
            {
                RPoney.Log.LoggerManager.Error(GetType().Name, $"授权回调出错,code:{code}", ex);
                return(View("_Error"));
            }
        }
コード例 #2
0
        protected T_ProductUserTokenEntity GetProductUserToken()
        {
            var result = _productUserTokenBll.Value.GetByResourceOwner(CurrentUser.ResourceOwner);

            if (null == result)
            {
                throw new Exception($"用户{CurrentUser.ResourceOwner}令牌信息不存在");
            }
            if (result.UpdateTime.AddSeconds(result.ExpiresIn) > DateTime.Now.AddSeconds(-120))
            {
                return(result);
            }
            //更新令牌
            GetTokenResultModel getToken = null;

            if (result.RefreshTokenTimeout.AddDays(-30) < DateTime.Now)
            {
                RPoney.Log.LoggerManager.Debug(GetType().Name, "更新刷新令牌");
                getToken = ApiCommon.GetToken(AppConfigBll.AppConfig.AppKey, AppConfigBll.AppConfig.AppSecrect, result.RefreshToken, result.AccessToken);
            }
            else
            {
                RPoney.Log.LoggerManager.Debug(GetType().Name, "使用刷新令牌换取accesstoken");
                getToken = ApiCommon.GetTokenByRefreshToKen(AppConfigBll.AppConfig.AppKey, AppConfigBll.AppConfig.AppSecrect, result.RefreshToken);
            }
            var productUserToken = new T_ProductUserTokenEntity
            {
                AliId               = getToken.AliId,
                MemberId            = getToken.MemberId,
                ResourceOwner       = getToken.ResourceOwner,
                AccessToken         = getToken.AccessToken,
                RefreshToken        = getToken.RefreshToken,
                ExpiresIn           = getToken.ExpiresIn.CInt(0, false),
                RefreshTokenTimeout = getToken.RefreshTokenTimeout.GetDateTimeFromUtc(DateTime.MinValue),
                UpdateTime          = DateTime.Now
            };

            if (_productUserTokenBll.Value.Save(productUserToken))
            {
                return(productUserToken);
            }
            return(null);
        }