Exemplo n.º 1
0
        /// <summary>
        /// 获取授权过的Access Token
        /// </summary>
        public Models.TencMToken GetAccessToken(string accessTokenUrl)
        {
            var token = new Models.TencMToken();
            if (HttpContext.Current.Request[OAuthStateKey] != null)
            {
                this.State = HttpContext.Current.Request[OAuthStateKey];
            }
            if (this.ResponseType == ResponseTypeEnum.code)
            {
                //response_type为code
                this.Code = HttpContext.Current.Request[OAuthCodeKey];
                this.OpenId = HttpContext.Current.Request[OAuthOpenIdKey];
                if (string.IsNullOrEmpty(this.Code))
                {
                    throw new ArgumentNullException(string.Format("{0} 为空值", OAuthCodeKey));
                }
                if (string.IsNullOrEmpty(this.OpenId))
                {
                    throw new ArgumentNullException(string.Format("{0} 为空值", OAuthOpenIdKey));
                }

            }
            else if (this.ResponseType == ResponseTypeEnum.token)
            {
                //response_type为token
                this.AccessToken = HttpContext.Current.Request[OAuthAccessTokenKey];
                this.ExpiresIn = int.TryParse(HttpContext.Current.Request[OAuthExpiresInKey], out _expires_in) ? _expires_in : 0;
                this.RefreshToken = HttpContext.Current.Request[OAuthRefreshTokenKey];
                this.OpenId = HttpContext.Current.Request[OAuthOpenIdKey];

                if (string.IsNullOrEmpty(this.OpenId) || ExpiresIn == 0 || string.IsNullOrEmpty(this.AccessToken))
                {
                    throw new ArgumentNullException(string.Format("{0},{1}或者{2} 为空值", OAuthOpenIdKey, OAuthAccessTokenKey, OAuthExpiresInKey));
                }
            }

            var paras = new NameValueCollection();
            paras.Add(OAuthClientIdKey, this.ClientId);
            paras.Add(OAuthClientSecretKey, this.ClientSecret);
            paras.Add(OAuthGrantTypeKey, this.GrantType.ToString());

            if (this.GrantType == GrantTypeEnum.authorization_code)
            {
                //grant_type为authorization_code时
                paras.Add(OAuthCodeKey, this.Code);
                paras.Add(OAuthRedirectUriKey, this.RedirectUri);
            }
            else if (this.GrantType == GrantTypeEnum.password)
            {
                //grant_type为password时
                paras.Add(OAuthUserNameKey, this.UserName);
                paras.Add(OAuthPasswordKey, this.Password);
            }
            else if (this.GrantType == GrantTypeEnum.refresh_token)
            {
                //grant_type为refresh_token时
                paras.Add(OAuthRefreshTokenKey, this.RefreshToken);
            }

            string response = Helpers.HttpHelper.HttpPost(accessTokenUrl, paras);
            if (!string.IsNullOrEmpty(response))
            {
                NameValueCollection qs = HttpUtility.ParseQueryString(response);

                if (qs["errorCode"] != null)
                {
                    token.ret = -1;
                    token.errcode = qs["errorCode"].ToString();
                    token.msg = qs["errorMsg"].ToString();
                }
                else
                {

                    if (qs[OAuthAccessTokenKey] != null)
                    {
                        this.AccessToken = qs[OAuthAccessTokenKey].ToString();
                        token.access_token = this.AccessToken;
                    }

                    if (qs[OAuthExpiresInKey] != null)
                    {
                        this.ExpiresIn = int.TryParse(qs[OAuthExpiresInKey].ToString(), out _expires_in) ? _expires_in : 0;

                        token.expires_in = this.ExpiresIn;
                    }
                    token.openid = this.OpenId;
                }
            }
            return token;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取授权过的Access Token
        /// </summary>
        public Models.TencMToken GetAccessToken(string accessTokenUrl)
        {
            var token = new Models.TencMToken();

            if (HttpContext.Current.Request[OAuthStateKey] != null)
            {
                this.State = HttpContext.Current.Request[OAuthStateKey];
            }
            if (this.ResponseType == ResponseTypeEnum.code)
            {
                //response_type为code
                this.Code   = HttpContext.Current.Request[OAuthCodeKey];
                this.OpenId = HttpContext.Current.Request[OAuthOpenIdKey];
                if (string.IsNullOrEmpty(this.Code))
                {
                    throw new ArgumentNullException(string.Format("{0} 为空值", OAuthCodeKey));
                }
                if (string.IsNullOrEmpty(this.OpenId))
                {
                    throw new ArgumentNullException(string.Format("{0} 为空值", OAuthOpenIdKey));
                }
            }
            else if (this.ResponseType == ResponseTypeEnum.token)
            {
                //response_type为token
                this.AccessToken  = HttpContext.Current.Request[OAuthAccessTokenKey];
                this.ExpiresIn    = int.TryParse(HttpContext.Current.Request[OAuthExpiresInKey], out _expires_in) ? _expires_in : 0;
                this.RefreshToken = HttpContext.Current.Request[OAuthRefreshTokenKey];
                this.OpenId       = HttpContext.Current.Request[OAuthOpenIdKey];

                if (string.IsNullOrEmpty(this.OpenId) || ExpiresIn == 0 || string.IsNullOrEmpty(this.AccessToken))
                {
                    throw new ArgumentNullException(string.Format("{0},{1}或者{2} 为空值", OAuthOpenIdKey, OAuthAccessTokenKey, OAuthExpiresInKey));
                }
            }

            var paras = new NameValueCollection();

            paras.Add(OAuthClientIdKey, this.ClientId);
            paras.Add(OAuthClientSecretKey, this.ClientSecret);
            paras.Add(OAuthGrantTypeKey, this.GrantType.ToString());

            if (this.GrantType == GrantTypeEnum.authorization_code)
            {
                //grant_type为authorization_code时
                paras.Add(OAuthCodeKey, this.Code);
                paras.Add(OAuthRedirectUriKey, this.RedirectUri);
            }
            else if (this.GrantType == GrantTypeEnum.password)
            {
                //grant_type为password时
                paras.Add(OAuthUserNameKey, this.UserName);
                paras.Add(OAuthPasswordKey, this.Password);
            }
            else if (this.GrantType == GrantTypeEnum.refresh_token)
            {
                //grant_type为refresh_token时
                paras.Add(OAuthRefreshTokenKey, this.RefreshToken);
            }

            string response = Helpers.HttpHelper.HttpPost(accessTokenUrl, paras);

            if (!string.IsNullOrEmpty(response))
            {
                NameValueCollection qs = HttpUtility.ParseQueryString(response);

                if (qs["errorCode"] != null)
                {
                    token.ret     = -1;
                    token.errcode = qs["errorCode"].ToString();
                    token.msg     = qs["errorMsg"].ToString();
                }
                else
                {
                    if (qs[OAuthAccessTokenKey] != null)
                    {
                        this.AccessToken   = qs[OAuthAccessTokenKey].ToString();
                        token.access_token = this.AccessToken;
                    }

                    if (qs[OAuthExpiresInKey] != null)
                    {
                        this.ExpiresIn = int.TryParse(qs[OAuthExpiresInKey].ToString(), out _expires_in) ? _expires_in : 0;

                        token.expires_in = this.ExpiresIn;
                    }
                    token.openid = this.OpenId;
                }
            }
            return(token);
        }