/// <summary> /// Send a Bad Request response with the OAuthRequestException details in the header /// and the response parameters in the body. /// </summary> /// <param name="context">HTTP context</param> /// <param name="exception">OAuth exception</param> /// <param name="responseParameters">Response parameters</param> public static void SendBadRequest(HttpContext context, OAuthRequestException exception, NameValueCollection responseParameters) { if (context == null) { throw new ArgumentNullException("context"); } if (exception == null) { throw new ArgumentNullException("exception"); } // There is a problem with the parameters; return 400 Bad Request context.Response.StatusCode = (int)HttpStatusCode.BadRequest; // Add the problem report in the WWW-Authenticate header context.Response.AddHeader( Constants.WwwAuthenticateHeaderParameter, exception.ToHeaderFormat(ServiceProviderContext.Settings.AuthenticationRealm)); // Write the response if (responseParameters != null && responseParameters.Count > 0) { context.Response.Write(Rfc3986.EncodeAndJoin(responseParameters)); } context.Response.End(); }
/// <summary> /// Send a OK response with the response parameters in the body. /// </summary> /// <param name="context">HTTP context</param> /// <param name="responseParameters">Response parameters</param> public static void SendOk(HttpContext context, NameValueCollection responseParameters) { if (context == null) { throw new ArgumentNullException("context"); } // There is a problem with the parameters; return 401 Bad Request context.Response.StatusCode = (int)HttpStatusCode.OK; // Write the response if (responseParameters != null && responseParameters.Count > 0) { context.Response.Write(Rfc3986.EncodeAndJoin(responseParameters)); } context.Response.End(); }
/// <summary> /// /// </summary> /// <param name="requestUri"></param> /// <param name="authParameters"></param> /// <param name="httpMethod"></param> /// <param name="contentType"></param> /// <param name="bodyStream"></param> /// <returns></returns> protected virtual HttpWebRequest CreateRequest(Uri requestUri, OAuthParameters authParameters, string httpMethod, string contentType, System.IO.Stream bodyStream) { NameValueCollection requestSpecificParameters = new NameValueCollection(authParameters.AdditionalParameters); if (!this.Service.UseAuthorizationHeader) { ////The OAuth params need to be added either into the querystring or into the post body. requestSpecificParameters.Add(authParameters.OAuthRequestParams()); } if (Constants.HttpPostUrlEncodedContentTypeRegex.IsMatch(contentType) && bodyStream == null) { ////All the requestSpecificParameters need to be encoded into the body bytes string body = Rfc3986.EncodeAndJoin(requestSpecificParameters); bodyStream = new MemoryStream(Encoding.ASCII.GetBytes(body)); } else { ////They go into the querystring. string query = Rfc3986.EncodeAndJoin(requestSpecificParameters); if (!string.IsNullOrEmpty(query)) { UriBuilder mutableRequestUri = new UriBuilder(requestUri); if (string.IsNullOrEmpty(mutableRequestUri.Query)) { mutableRequestUri.Query = query; } else { mutableRequestUri.Query = mutableRequestUri.Query.Substring(1) + "&" + query; } requestUri = mutableRequestUri.Uri; } } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri); request.Method = httpMethod; if (this.Service.UseAuthorizationHeader) { request.Headers.Add(HttpRequestHeader.Authorization, authParameters.ToHeaderFormat()); } if (!String.IsNullOrEmpty(contentType)) { request.ContentType = contentType; if (bodyStream != null) { if (bodyStream.CanSeek) { request.ContentLength = bodyStream.Length; } StreamCopier.CopyTo(bodyStream, request.GetRequestStream()); } } return(request); }