Пример #1
0
        /// <summary>
        /// 获取当前账号的AccessToken
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public AccessToken GetAccessToken(int wid, out string errorMessage)
        {
            lock (_obj)
            {
                try
                {
                    var appInfo = _appInfoRepository.GetAppInfo(wid);
                    if (appInfo == null)
                    {
                        errorMessage = string.Format("当前系统无wid为{0}相应公众号的配置", wid);
                        return(null);
                    }
                    if (string.IsNullOrEmpty(appInfo.AppId) || string.IsNullOrEmpty(appInfo.AppSecret))
                    {
                        errorMessage = string.Format("当前系统wid为{0}相应公众号的配置不完整,AppId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!",
                                                     wid);
                        return(null);
                    }

                    //从数据库中获取Token
                    var tokenFromDb = _accessTokenRepository.GetAccessToken(appInfo.Id);
                    //如果不存在
                    if (tokenFromDb == null || tokenFromDb.IsExpired())
                    {
                        //设置已存的AccessToken过期
                        _accessTokenRepository.SetAccessTokenExpire(tokenFromDb);

                        //从微信重新获取AccessToken
                        var accessTokenResult = CommonApi.GetToken(appInfo.AppId, appInfo.AppSecret);
                        var newAccessToken    = new AccessToken
                        {
                            Id          = Guid.NewGuid(),
                            AppId       = appInfo.Id,
                            SysDateTime = DateTime.Now,
                            Ticket      = accessTokenResult.access_token,
                            Expires     = accessTokenResult.expires_in
                        };

                        //缓存新获取到的AccessToken到数据库
                        _accessTokenRepository.Add(newAccessToken);

                        errorMessage = string.Empty;
                        return(newAccessToken);
                    }

                    errorMessage = string.Empty;

                    return(tokenFromDb);
                }
                catch (Exception exception)
                {
                    errorMessage = exception.Message;
                    return(null);
                }
            }
        }
Пример #2
0
        public async Task <JwtToken> GenerateTokenAsync(IResourceOwnerPasswordCredentialsTokenRequest request)
        {
            await ValidateRequestAsync(request);

            var jwtToken = await CreateJwtTokenAsync();

            var accessToken = jwtToken.ToAccessToken();

            _accessTokenRepository.Add(accessToken);
            await _accessTokenRepository.SaveAsync();

            return(jwtToken);
        }
Пример #3
0
        public async Task <JwtToken> GenerateTokenAsync(IRefreshTokenRequest request)
        {
            await ValidateRequestAsync(request);

            var refreshToken = GenerateRefreshToken();
            var jwtToken     = await CreateJwtTokenAsync();

            jwtToken.RefreshToken      = refreshToken.RefreshTokenId;
            refreshToken.AccessTokenId = jwtToken.TokenId;
            refreshToken.Code          = _refreshToken.Code;
            _refreshToken.Used         = true;
            var accessToken = jwtToken.ToAccessToken();

            _accessTokenRepository.Add(accessToken);

            await SaveAsync();

            return(jwtToken);
        }