I am trying to create a RESTful web service based on WCF Web API. I also need to control access using OAuth and for this I am using the DotNetOpenAuth open source library.
Has anyone ever been successful integrating the two? I am struggling converting from the HTTP entities representations of the WCF Web API into something that's understandable by DNOA (e.g. HTTP requests, HTTP headers, etc...).
Any tip would be greatly appreciated.
Could you be a little more specific?
In WebAPI a request is represented by the HttpRequestMessage class. A response is represented by the HttpResponseMessage class.
I've no previous knowledge of DNOA, but from what I saw, you can easily create a HttpRequestInfo from an HttpRequestMessage using the public HttpRequestInfo(string httpMethod, Uri requestUrl, string rawUrl, WebHeaderCollection headers, Stream inputStream).
The HTTP method and request uri are directly HttpRequestMessage properties.
The input stream is obtained via the Content property. I don't see a direct way of creating a WebHeaderCollection from the WebAPI's HttpRequestHeaders. However, you can iterate the HttpRequestHeaders entries and insert then on the WebHeaderCollection one by one.
Related
Is it possible to populate request headers with custom headers in middleware? My aim is making calls to external rest api which has basic auth implemented. So it would be nice to keep API secrets in back-end code.
I have tried all methods that possibly could add header to request headers in HTTP context but seems it is a bit harder then I thought before.
There is no problems to modify response headers maybe there is such way with request headers?
There is a difference between modifying requests and responses here.
Since you're calling a rest api you need to inject an IHttpClientFactory > Create a client > Add the auth headers to it via appsettings.json (for example)
or
You can use Typed Clients which can be configured like this:
services.AddHttpClient<GithubClient>(c => { c.DefaultRequestHeaders.Add("Bearer", "token"); });
You can add your auth headers from the config above using the DefaultRequestHeaders property then inject an HttpClient in the your typed service which will be responsible for making the calls to your rest api.
Everything about this topic is available in the docs. See here
I am currently trying to write a portable class library in c# that will allow me to send SOAP POST requests to a web service.
Because you are limited in what you can reference when making a PCL, I cannot simply "Add a service reference." Instead, I am trying to manually make the SOAP request and I am getting stuck on adding headers to the HttpWebRequest.
My code is as follows:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("website_here");
byte[] xmlStr = getPullCustInfoXml(postParameters);
request.Method = "POST";
request.Headers.Add() <- trying to add headers here.
I can't seem to add any headers because I get the error:
'System.Net.Http.WebHeaderCollectionExtensions.Add(System.Net.WebHeaderCollection, string, string)' is inaccessible due to its protection level
Why would it be inaccessible even after I casted the request as an HttpWebRequest?
Is there a better way to implement SOAP requests when making a portable class library?
I don't get the same error message when I copy and paste your code.
Make sure HttpWebRequest is from System.Net namespace though. The documentation says Headers.Add methods are all public.
Also, try different overload methods. There are:
Add(NameValueCollection)
Add(String)
Add(HttpRequestHeader, String)
Add(HttpResponseHeader, String)
Add(String, String)
More info: http://msdn.microsoft.com/en-us/library/system.net.webheadercollection(v=vs.110).aspx
I'm working on a proxy that will relay connections to another server. I had implemented a IHttpHandler that takes requests for a specific address and sends them to my proxy.
My proxy basically starts a sockect connection to the proxied server and reads the original request:
var requestString = new StreamReader(httpRequest.InputStream).ReadToEnd();
My problem arises at this point: the input stream only contains the stream of the body of the HTTP request, not the complete request.
How can I retrieve the full HTTP Request without having to reconstruct it from the HttpRequest object?
I don't know of a definitive one-liner in ASP .NET, but you can get pretty close, as shown in this forum post. What is suggested there is to build the first line out of HttpRequest properties, then add ServerVariables["ALL_RAW"], then the request body in the InputStream as you're already doing.
The only way to get the 'full HTTP Request' object is to actually reconstruct it in some way. I'm not sure you need the whole object for this process though. Perhaps you could just grab a subset of the items in HttpRequest for processing? You could manually concatenate the various portions of the object you need into something new. Once you deviate from using HttpRequest though, it's all custom to me.
I have a web page that intercepts POST requests, pulls the username out of the request, and is supposed to forward the request on depending on the username. Now, I notice that the incoming HttpRequest has a Params property, and HttpWebRequest does not. Why is this? Is there a way I can capture the Params data in my outgoing HttpWebRequest?
Thanks.
They're simply two different .Net classes in two different packages:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.71%29.aspx
System.Object
System.MarshalByRefObject
System.Net.WebRequest
System.Net.HttpWebRequest
http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx
System.Object
System.Web.HttpRequest
HttpWebRequest is an old .Net 1.1 thing - I would definitely use Web.HttpRequest if you're planning on refactoring any code (or writing any new code!)
IMHO...
For starters, they're completely different classes, in different namespaces. That being said,
Params is a wrapper that exposes both querystring parameters as well as POST data. When you are construcing a request you can't write to params, you have to specify what kind of data you are actually adding. So you should add it to the target url as a querystring or you can add it to the body of the request and make it a POST.
There is a web service that can only be consumed via http and a client that can only consume https web services. Therefore I need some intermediary that forwards on requests received in https and returns responses in http.
Supposing that the intermediary is completely dumb save for the fact that it knows where the web service endpoint is (i.e. it doesn't know what the signature of the service is, it just knows that it can communicate with it via an http web request, and it listens on some https uri, forwarding on anything it receives), what is the most simple way of achieving this?
I've been playing around with this all day and am not sure how to achieve the "dumb" bit, i.e. not knowing the signature for passing back the verbatim response.
A dumb intermediary is essentially a proxy. Your best bet might to be just use standard asp.net pages (instead of shoehorning into service functionality like ASMX or WCF which are just going to fight you) so you can receive the request exactly as-is and process it in a simple way using standard request/response. You can make use of HttpWebRequest class to forward the request on to the other endpoint.
Client requests https://myserver.com/forwarder.aspx?forwardUrl=http://3rdparty.com/api/login
myserver.com (your proxy) reads querystring forwardUrl and any POST or GET request included.
myserver.com requests to http://3rdparty.com/api/login and passes along GET or POST data sent from the client.
myserver.com takes response and sends back as response to other endpoint (essentially just Response.Write contents out to the response)
You would need to write forwarder.aspx to process the requests. Code for forwarder.aspx would be something like this (untested):
protected void Page_Load(object sender, EventArgs e)
{
var forwardUrl = Request.QueryString["forwardUrl"];
var post = new StreamReader(Request.InputStream).ReadToEnd();
var req = (HttpWebRequest) HttpWebRequest.Create(forwardUrl);
new StreamWriter(req.GetRequestStream()).Write(post);
var resp = (HttpWebResponse)req.GetResponse();
var result = new StreamReader(resp.GetResponseStream).ReadToEnd();
Response.Write(result); // send result back to caller
}