internal string GenerateAbsoluteUrl(string relative, NameValueCollection query) { string url = "https://api.github.com"; if (Credentials != null) { url = "https://" + Credentials.UserName + ":" + Credentials.Password + "@api.github.com"; } else if (AccessToken != null) { query.Add("access_token", AccessToken); } // Add the relative URL url += relative; // Append the query string (if not empty) if (query != null && query.Count > 0) { url += "?" + SocialUtils.NameValueCollectionToQueryString(query); } // Now return the URL return(url); }
public SocialHttpResponse GetResponse() { // Merge the query string string url = new UriBuilder(Url).MergeQueryString(QueryString).ToString(); // Initialize the request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // Set the method request.Method = Method; request.Credentials = Credentials; // Add any headers // TODO: Add headers // Add the request body (if a POST request) if (Method == "POST" && PostData != null && PostData.Count > 0) { string dataString = SocialUtils.NameValueCollectionToQueryString(PostData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = dataString.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length); } } // Get the response try { return(new SocialHttpResponse((HttpWebResponse)request.GetResponse())); } catch (WebException ex) { return(new SocialHttpResponse((HttpWebResponse)ex.Response, ex)); } }
public string GetAuthorizationUrl(string state, GitHubScopeCollection scopes) { // Initialize the query string NameValueCollection nvc = new NameValueCollection { { "client_id", ClientId } }; // Add the redirect URI if specified if (!String.IsNullOrWhiteSpace(RedirectUri)) { nvc.Add("redirect_uri", RedirectUri); } // Add the state if specified if (!String.IsNullOrWhiteSpace(state)) { nvc.Add("state", state); } // Get the scope list string scope = (scopes == null ? "" : scopes.ToString()); if (!String.IsNullOrWhiteSpace(scope)) { nvc.Add("scope", scope); } // Generate the URL return("https://github.com/login/oauth/authorize?" + SocialUtils.NameValueCollectionToQueryString(nvc)); }
/// <summary> /// Makes a signed request to the Twitter API based on the specified parameters. /// </summary> /// <param name="method">The HTTP method of the request.</param> /// <param name="url">The base URL of the request (no query string).</param> /// <param name="queryString">The query string.</param> /// <param name="postData">The POST data.</param> public virtual HttpWebResponse DoHttpRequest(string method, string url, NameValueCollection queryString, NameValueCollection postData) { // Check if NULL if (queryString == null) { queryString = new NameValueCollection(); } if (postData == null) { postData = new NameValueCollection(); } // Merge the query string if (queryString.Count > 0) { UriBuilder builder = new UriBuilder(url); url += (url.Contains("?") ? "&" : "") + builder.MergeQueryString(queryString).Query; } // Initialize the request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // Generate the signature string signature = GenerateSignature(method, url, queryString, postData); // Generate the header string header = GenerateHeaderString(signature); // Add the authorization header request.Headers.Add("Authorization", header); // Set the method request.Method = method; // Add the request body (if a POST request) if (method == "POST" && postData.Count > 0) { string dataString = SocialUtils.NameValueCollectionToQueryString(postData); //throw new Exception(dataString); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = dataString.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length); } } // Make sure we reset the client (timestamp and nonce) if (AutoReset) { Reset(); } // Get the response try { return((HttpWebResponse)request.GetResponse()); } catch (WebException ex) { return((HttpWebResponse)ex.Response); } }
public static UriBuilder MergeQueryString(this UriBuilder builder, NameValueCollection values) { if (values == null || values.Count == 0) { return(builder); } builder.Query = SocialUtils.NameValueCollectionToQueryString(HttpUtility.ParseQueryString(builder.Query).Set(values)); return(builder); }
public static void AppendQueryString(this UriBuilder builder, NameValueCollection values) { if (values == null || values.Count == 0) { return; } NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query); nvc.Add(values); builder.Query = SocialUtils.NameValueCollectionToQueryString(nvc); }
public SocialHttpResponse GetResponse() { // Build the URL string url = Url; if (QueryString != null && !QueryString.IsEmpty) { url += (url.Contains("?") ? "&" : "?") + QueryString; } // Initialize the request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // Misc request.Method = Method; request.Credentials = Credentials; request.Headers = Headers.Headers; request.Accept = Accept; request.Timeout = (int)Timeout.TotalMilliseconds; // Add the request body (if a POST request) if (Method == "POST" && PostData != null && PostData.Count > 0) { string dataString = SocialUtils.NameValueCollectionToQueryString(PostData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = dataString.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length); } } // Get the response try { return(SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse)); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.ProtocolError) { throw; } return(SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse)); } }
public string GenerateUrl(string url, NameValueCollection query) { return(url + (query.Count == 0 ? "" : "?" + SocialUtils.NameValueCollectionToQueryString(query))); }
public string GetAuthorizationUrl(string state, GitHubScope scope = GitHubScope.Default) { // Initialize the query string NameValueCollection nvc = new NameValueCollection { { "client_id", ClientId } }; // Add the redirect URI if specified if (!String.IsNullOrWhiteSpace(RedirectUri)) { nvc.Add("redirect_uri", RedirectUri); } // Add the state if specified if (!String.IsNullOrWhiteSpace(state)) { nvc.Add("state", state); } // Get the scope list List <string> scopes = new List <string>(); if (scope.HasFlag(GitHubScope.User)) { scopes.Add("user"); } if (scope.HasFlag(GitHubScope.UserEmail)) { scopes.Add("user:email"); } if (scope.HasFlag(GitHubScope.UserFollow)) { scopes.Add("user:follow"); } if (scope.HasFlag(GitHubScope.PublicRepo)) { scopes.Add("public_repo"); } if (scope.HasFlag(GitHubScope.Repo)) { scopes.Add("repo"); } if (scope.HasFlag(GitHubScope.RepoStatus)) { scopes.Add("repo:status"); } if (scope.HasFlag(GitHubScope.DeleteRepo)) { scopes.Add("delete_repo"); } if (scope.HasFlag(GitHubScope.Notifications)) { scopes.Add("notifications"); } if (scope.HasFlag(GitHubScope.Gist)) { scopes.Add("gist"); } if (scopes.Count > 0) { nvc.Add("scope", String.Join(",", scopes)); } // Generate the URL return("https://github.com/login/oauth/authorize?" + SocialUtils.NameValueCollectionToQueryString(nvc)); }
protected void Page_Load(object sender, EventArgs e) { //Get current user var currentUser = UmbracoContext.Current.Security.CurrentUser; //Check a user is logged into backoffice if (currentUser == null) { //Ouput an error message Content.Text += ui.Text("analytics", "noAccess"); return; } //Check the user has access to the analytics section //Prevents anyone with this URL to page from just hitting it if (!currentUser.AllowedSections.Contains("analytics")) { //Ouput an error message Content.Text += ui.Text("analytics", "noAccess"); return; } // The Analytics authentication site should redirect back to this URI string callback = Request.Url.AbsoluteUri.Replace("/OAuth.aspx", "/OAuthCallback.aspx"); // Generate a random state string state = Guid.NewGuid().ToString(); // Add the state to the session Session.Add("Analytics_" + state, "#h5yr"); // The query string to send to the authentication site NameValueCollection nvc = new NameValueCollection { { "clientcallback", callback }, { "clientstate", state }, { "lang", UmbracoContext.Current.Security.CurrentUser.Language } }; // Generate the URL for the authentication page string oAuthUrl = "http://analytics-oauth.azurewebsites.net/callback/OAuth.aspx?" + SocialUtils.NameValueCollectionToQueryString(nvc); // Now redirect the user Response.Redirect(oAuthUrl); }
public override string ToString() { return(SocialUtils.NameValueCollectionToQueryString(_nvc)); }
protected void Page_Load(object sender, EventArgs e) { // The Analytics authentication site should redirect back to this URI string callback = Request.Url.AbsoluteUri.Replace("/OAuth.aspx", "/OAuthCallback.aspx"); // Generate a random state string state = Guid.NewGuid().ToString(); // Add the state to the session Session.Add("Analytics_" + state, "#h5yr"); // The query string to send to the authentication site NameValueCollection nvc = new NameValueCollection { { "clientcallback", callback }, { "clientstate", state } }; // Generate the URL for the authentication page string oAuthUrl = "http://analytics-oauth.azurewebsites.net/callback/OAuth.aspx?" + SocialUtils.NameValueCollectionToQueryString(nvc); // Now redirect the user Response.Redirect(oAuthUrl); }