Example #1
0
 public virtual string GetSimulateLoginPostBody(string passport, string password)
 {
     var dict2 = new Dictionary<string, string>();
     dict2.Add("action", "submit");
     dict2.Add("withOfficalFlag", "0");
     dict2.Add("ticket", null);
     dict2.Add("isLoginSina", null);
     dict2.Add("response_type", "token");
     dict2.Add("regCallback", null);
     dict2.Add("redirect_uri", Settings.CallbackUrl);
     dict2.Add("client_id", Settings.AppKey);
     dict2.Add("state", null);
     dict2.Add("from", null);
     dict2.Add("userId", passport);
     dict2.Add("passwd", password);
     dict2.Add("display", "js");
     return dict2.BuildToUrlString();
 }
Example #2
0
 /// <summary>
 /// Get AuthorizeUrl
 /// </summary>
 /// <remarks>Firstly, should be get the authorization.</remarks>
 /// <returns></returns>
 public string GetAuthorizeUrl()
 {
     var dict = new Dictionary<string, string>();
     dict.Add("client_id", Settings.AppKey);
     dict.Add("redirect_uri", Settings.CallbackUrl);
     dict.Add("response_type", "code");
     //dict1.Add("display", "default");
     return Settings.AuthorizeUrl + "?" + dict.BuildToUrlString();
 }
Example #3
0
        /// <summary>
        /// Get AccessToken Async
        /// </summary>
        /// <param name="grantType">authorization_code、password、refresh_token</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        internal async Task<OAuthAccessToken> GetAccessTokenAsync(GrantType grantType, Dictionary<string, string> parameters)
        {
            if (parameters == null || parameters.Count == 0)
            {
                throw new ArgumentException("GetAccessTokenAsync Parameters is invalid.");
            }

            HttpWebRequest request = null;
            try
            {
                request = WebRequest.Create(Settings.AccessTokenUrl) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = new CookieContainer();

                parameters.Add("client_id", Settings.AppKey);
                parameters.Add("client_secret", Settings.AppSecret);

                var grant_type = "";
                switch (grantType)
                {
                    case GrantType.AuthorizationCode:
                        grant_type = "authorization_code";
                        break;
                    case GrantType.Password:
                        grant_type = "password";
                        break;
                    case GrantType.RefreshToken:
                        grant_type = "refresh_token";
                        break;
                    default:
                        grant_type = "authorization_code";
                        break;
                }
                parameters.Add("grant_type", grant_type);

                var postBody = parameters.BuildToUrlString();
                var postData = System.Text.Encoding.UTF8.GetBytes(postBody);
                using (var requestStream = await request.GetRequestStreamAsync())
                {
                    await requestStream.WriteAsync(postData, 0, postData.Length);
                }
                using (var response = await request.GetResponseAsync())
                {
                    using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        var result = await sr.ReadToEndAsync();
                        return ConvertToAccessTokenByRegex(result);
                    }
                }
            }
            finally
            {
                if (request != null)
                    request.Abort();
            }
        }
Example #4
0
        public async Task<int> ExpiresInAccessTokenAsync(string accessToken)
        {
            HttpWebRequest request = null;
            try
            {
                request = WebRequest.Create(Settings.QueryAccessTokenUrl) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = new CookieContainer();

                var parameters = new Dictionary<string, string>();
                parameters.Add("access_token", accessToken);

                var postBody = parameters.BuildToUrlString();
                var postData = System.Text.Encoding.UTF8.GetBytes(postBody);

                using (var requestStream = await request.GetRequestStreamAsync())
                {
                    await requestStream.WriteAsync(postData, 0, postData.Length);
                }

                var expiresIn = 0;
                using (var response = await request.GetResponseAsync())
                {
                    using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        var result = await sr.ReadToEndAsync();
                        var regex = new Regex(Constants.OAuth2AccessTokenExpiresInPattern);
                        var match = regex.Match(result);
                        if (match == null)
                        {
                            return expiresIn;
                        }
                        var strExpiresIn = match.Groups["expires_in"].Value;
                        var parseResult = int.TryParse(strExpiresIn, out expiresIn);
                        return expiresIn;
                    }
                }
            }
            finally
            {
                if (request != null)
                    request.Abort();
            }
        }