Ejemplo n.º 1
0
        /// <summary>
        /// 检查AccessToken是否过期.
        /// </summary>
        private void CheckTokenValid()
        {
            try
            {
                if (localSettings.Values.ContainsKey(Constants.ACCESSTOKEN) == false)
                {
                    IsAuthorized = false;
                }
                else
                {
                    if (localSettings.Values.ContainsKey(Constants.EXPIREDTIME))
                    {
                        var expiredTime   = (long)localSettings.Values[Constants.EXPIREDTIME];
                        var lastOAuthTime = (long)localSettings.Values[Constants.LASTAUTHTIME];
                        var nowTime       = Unix2DateTime.GetUnixTimestamp();

                        if (nowTime >= lastOAuthTime + expiredTime)
                        {
                            IsAuthorized = false;
                        }
                        else
                        {
                            SdkData.AccessToken = localSettings.Values[Constants.ACCESSTOKEN] as string;
                            IsAuthorized        = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                IsAuthorized = false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 处理授权结果.
        /// </summary>
        /// <param name="response"></param>
        private void HandleResult(RestResponse response)
        {
            SdkAuthError err         = new SdkAuthError();
            string       responseStr = response.Content;

            if (response.StatusCode != (int)HttpStatusCode.OK || response.ErrorException != null)
            {
                if (response.ContentLength == 0D)
                {
                    err.errCode = SdkErrCode.NET_UNUSUAL;
                    if (null != LoginCallback)
                    {
                        LoginCallback(false, err, null);
                    }
                    return;
                }
                // 解析错误信息.
                OAuthErrRes errRes = SerializeOAuthResult <OAuthErrRes>(responseStr);

                err.errCode      = SdkErrCode.SERVER_ERR;
                err.specificCode = errRes.ErrorCode;
                err.errMessage   = errRes.errDes;

                if (null != LoginCallback)
                {
                    LoginCallback(false, err, null);
                }
            }
            else
            {
                err.errCode = SdkErrCode.SUCCESS;
                SdkAuth2Res oauthResult = SerializeOAuthResult <SdkAuth2Res>(responseStr);

                // 保存AccessToken.
                SdkData.AccessToken = oauthResult.AccessToken;
                localSettings.Values[Constants.ACCESSTOKEN]  = oauthResult.AccessToken;
                localSettings.Values[Constants.EXPIREDTIME]  = string.IsNullOrEmpty(oauthResult.ExpriesIn) ? 0: long.Parse(oauthResult.ExpriesIn);
                localSettings.Values[Constants.LASTAUTHTIME] = Unix2DateTime.GetUnixTimestamp();
                if (oauthResult.RefreshToken != null)
                {
                    localSettings.Values[Constants.REFRESHTOKEN] = oauthResult.RefreshToken;
                }

                if (null != LoginCallback)
                {
                    LoginCallback(true, err, oauthResult);
                }
            }
        }