public HttpRequestMessage ToHttpRequestMessage()
            {
                var query = new Dictionary <string, string>()
                {
                    { "grant_type", GrantType.ToString() },
                    { "redirect_uri", RedirectUri },
                    { "client_id", ClientId },
                    { "client_secret", ClientSecret },
                };

                switch (GrantType)
                {
                case GrantType.authorization_code:
                    query.Add("code", Code);
                    break;

                case GrantType.refresh_token:
                    query.Add("refresh_token", RefreshToken);
                    break;
                }
                var message = new HttpRequestMessage(HttpMethod.Post, Constants.URL.API.OAuth.Token)
                {
                    Content = new FormUrlEncodedContent(query), // Content-Type: application/x-www-form-urlencoded
                };

                message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                return(message);
            }
Exemple #2
0
        public static string GetDescription(this GrantType token)
        {
            Type type = typeof(GrantType);

            //Tries to find a DescriptionAttribute for a potential friendly name
            //for the enum
            MemberInfo[] memberInfo = type.GetMember(token.ToString());
            var          attribute  = memberInfo[0].GetCustomAttribute <DescriptionAttribute>(false);

            //Pull out the description value
            return(attribute.Description);
        }
        private static NameValueCollection GetNameValueCollection(string clientId, string clientSecret,
                                                                  string grantData, GrantType grantType)
        {
            var parameters = new NameValueCollection();

            parameters.Add("client_id", clientId);
            parameters.Add("client_secret", clientSecret);
            if (grantType == GrantType.authorization_code)
            {
                parameters.Add("code", grantData);
            }
            else
            {
                parameters.Add("refresh_token", grantData);
            }
            parameters.Add("grant_type", grantType.ToString());

            return(parameters);
        }
        public string GetWeChatAccessTokenUrl(string requestUrl, GrantType grantType, string code, string refreshToken, string redirectUrl)
        {
            requestUrl += "token.format";

            if (_postArgumentList == null)
                _postArgumentList = new List<KeyValuePair<string, object>>();

            _postArgumentList.Add(new KeyValuePair<string, object>("grant_type", grantType.ToString().ToLower()));

            if (grantType == GrantType.AuthenticationCode)
            {
                //code 
                _postArgumentList.Add(new KeyValuePair<string, object>("code", code));
                _postArgumentList.Add(new KeyValuePair<string, object>("redirect_uri", redirectUrl));
            }
            else
                _postArgumentList.Add(new KeyValuePair<string, object>("refresh_token", refreshToken));
            return requestUrl;
        }
Exemple #5
0
        private async Task <AccessToken> GetAccessToken(string token, GrantType t)
        {
            List <KeyValuePair <string, string> > k = new List <KeyValuePair <string, string> >();

            k.Add(new KeyValuePair <string, string>(t == GrantType.authorization_code ? "code" : "refresh_token", token));
            k.Add(new KeyValuePair <string, string>("client_id", "409828063726.apps.googleusercontent.com"));
            k.Add(new KeyValuePair <string, string>("client_secret", "maIX_et6wkJ5sqDcSjeGJRWZ"));
            if (t == GrantType.authorization_code)
            {
                k.Add(new KeyValuePair <string, string>("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"));
            }
            k.Add(new KeyValuePair <string, string>("grant_type", t.ToString()));

            string response = await Util.MakeWebPostRequestAsync("https://accounts.google.com/o/oauth2/token", k);

            AccessToken at = JsonConvert.DeserializeObject <AccessToken>(response);

            return(at);
        }
Exemple #6
0
        public virtual OAuthAccessToken GetAccessToken(string code, GrantType grantType)
        {
            var args = new FunctionArguments
            {
                ConsumerKey    = _consumerKey,
                ConsumerSecret = _consumerSecret,
            };

            var request = _accessTokenQuery.Invoke(args);

            request.AddParameter("client_id", _consumerKey);
            request.AddParameter("client_secret", _consumerSecret);
            request.AddParameter("grant_type ", grantType.ToString());

            if (grantType == GrantType.AuthorizationCode)
            {
                request.AddParameter("code", code);
                request.AddParameter("redirect_uri", _callbackUrl);
            }
            else
            {
                request.AddParameter("username", _userName);
                request.AddParameter("password", _passWord);
            }

            var response = _oauth.Request(request);

            SetResponse(response);

            ////var query = HttpUtility.ParseQueryString(response.Content);
            var query = JObject.Parse(response.Content);
            var oauth = new OAuthAccessToken()
            {
                Token     = query["access_token"].ToString() ?? string.Empty,
                ExpiresIn = query["expires_in"].ToString().ToInt32(),
                UserId    = query["uid"].ToString().ToInt64()
            };

            return(oauth);
        }
        public string GetWeChatAccessTokenUrl(string requestUrl, GrantType grantType, string code, string refreshToken, string redirectUrl)
        {
            requestUrl += "token.format";

            if (_postArgumentList == null)
            {
                _postArgumentList = new List <KeyValuePair <string, object> >();
            }

            _postArgumentList.Add(new KeyValuePair <string, object>("grant_type", grantType.ToString().ToLower()));

            if (grantType == GrantType.AuthenticationCode)
            {
                //code
                _postArgumentList.Add(new KeyValuePair <string, object>("code", code));
                _postArgumentList.Add(new KeyValuePair <string, object>("redirect_uri", redirectUrl));
            }
            else
            {
                _postArgumentList.Add(new KeyValuePair <string, object>("refresh_token", refreshToken));
            }
            return(requestUrl);
        }
Exemple #8
0
        public HttpContent GetPostContent()
        {
            if (String.IsNullOrEmpty(this.Code))
            {
                throw new ArgumentNullException("Code cannot be empty");
            }

            StringBuilder postData = new StringBuilder();

            postData.Append(HttpUtility.UrlEncode("client_id") + "=" + HttpUtility.UrlEncode(this.ClientId));
            postData.Append("&" + HttpUtility.UrlEncode("client_secret") + "=" + HttpUtility.UrlEncode(this.ClientSecret));
            postData.Append("&" + HttpUtility.UrlEncode("redirect_uri") + "=" + HttpUtility.UrlEncode("http://localhost"));
            postData.Append("&" + HttpUtility.UrlEncode("grant_type") + "=" + HttpUtility.UrlEncode(GrantType.ToString()));
            postData.Append("&" + HttpUtility.UrlEncode("code") + "=" + HttpUtility.UrlEncode(this.Code));

            return(new StringContent(postData.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded"));
        }
Exemple #9
0
 public string GetCreateRemoveScript(bool create)
 {
     return(string.Format(@"{0} {1} ON {2} {5} [{3}] {4}",
                          create ?
                          (GrantType == GrantType.GRANT_WITH_GRANT_OPTION ? GrantType.GRANT.ToString() : GrantType.ToString())
                                                 : "REVOKE",
                          PermissionType,
                          PermissionDescription == PermissionDescription.SCHEMA ?
                          string.Format("SCHEMA::[{0}]", Permission.PermissionSchemaName) :
                          string.Format("[{0}].[{1}]", Permission.SchemaName, Permission.PermissionName),
                          DatbasePrincipal.PrincipalName,
                          create ? (GrantType == GrantType.GRANT_WITH_GRANT_OPTION ? "WITH GRANT OPTION" : string.Empty)
                                                 : "CASCADE",
                          create ? "TO" : "FROM"));
 }
Exemple #10
0
 public async Task LoginWithSocialAsync(object context, GrantType socialProvider)
 {
     var renderer = (Auth0LoginPageRenderer)context;
     await _auth0Client.LoginAsync(renderer.Context, socialProvider.ToString(), true);
 }
Exemple #11
0
        /// <summary>
        /// 使用code换取access_token和openid
        /// </summary>
        /// <param name="appId">appId</param>
        /// <param name="secret">appKey</param>
        /// <param name="code">code</param>
        /// <param name="grantType"></param>
        /// <returns></returns>
        public static async Task <GetAccessTokenResponse> GetAccessToken(string appId, string secret, string code, GrantType grantType = GrantType.authorization_code)
        {
            HttpClient          httpClient = new HttpClient();
            HttpResponseMessage response   = await httpClient.GetAsync(
                "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId +
                "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code" + grantType.ToString());

            if (response.IsSuccessStatusCode)
            {
                string str = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <GetAccessTokenResponse>(str));
            }
            return(new GetAccessTokenResponse {
            });
        }
 public async Task LoginWithSocialAsync(object context, GrantType socialProvider)
 {
     var controller = (UIViewController)context;
     await _auth0Client.LoginAsync(controller, socialProvider.ToString(), true);
 }