How can I send an HTTP request with Negotiate Authorization header attribute from a .NET (C#) application?
I tried the following, but Authorization attribute was not added to the request...
...
string url = ...;
WebRequest request = WebRequest.Create(url);
request.Credentials = GetCredential();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
...
private CredentialCache GetCredential()
{
string url = ...;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Ntlm", new NetworkCredential(username, pwd, domain));
return credentialCache;
}
My experience with using a network credential in a WebRequest is that the request.GetResponse() does NOT pass the credential unless it receives an Unauthorized (challenge) response from the server. If it does receive a 403, it will automatically fire a second request which includes the credential. Make sure the end point you are hitting returns a 401 if the Auth header is missing.
Related
I have a third party URL which sends the oAuth parameters in the header. When i am reading it using the below code the Authorization parameters are not being returned. What am i missing?
string url = "http: // xxx.com/events/abc-def";
HttpClient httpClient = new HttpClient();
WebRequest request = WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
string authHeader = request.Headers["Authorization"];
I need to make a GET request to following url
[ ~ ] $ curl -u duff:X https://subs.pinpayments.com/api/v4/sitename/subscribers/7388.xml
where -u is username and then X is password.
How to use WebRequest?
Please suggest
The WebRequest class has a Credentials property, which you can set:
WebRequest request = WebRequest.Create(uri);
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Another possibility would be to use the WebClient class, that supports custom credentials too:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
//Byte[] pageData = client.DownloadData(url);
//string pageHtml = Encoding.ASCII.GetString(pageHtml);
// or DownloadString: http://msdn.microsoft.com/en-us/library/fhd1f0sw%28v=vs.110%29.aspx
var pageHtml = client.DownloadString(uri);
Console.WriteLine(pageHtml);
If you need for a reason to set custom header information for the request, then the WebClient class could be more suitable.
I have a REST-based web service I'm trying to connect to, and while it works just fine from my local dev machine, I am trouble getting it to work on my client's test system which is behind a web proxy.
I have this code snippet here:
HttpWebRequest request = WebRequest.Create(targetUrl) as HttpWebRequest;
if (request == null)
return;
request.Method = "GET";
request.Accept = "application/json";
request.Credentials = new NetworkCredential("myusername", "mytopsecretpassword");
WebProxy webProxy = new WebProxy("myproxy.net", 8080)
{
Credentials = new NetworkCredential("myusername", "mytopsecretpassword"),
UseDefaultCredentials = false
};
request.Proxy = webProxy;
But when I try to execute this call and get back the response like this:
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
I keep getting an error:
System.Net.WebException
The remote server returned an error: (407) Proxy Authentication Required.
WTF ?!?! I'm setting up my proxy and I'm providing the proxy credentials.... what more can I do?
Try adding the domain to the proxy credentials.
I'd also try setting UseDefaultCredentials = false before you set the new credentials.
I am running the following in c# to send a login request to an API. However, according to Fiddler, it is not sending an Authentication Header. I am a rookie, so it is likely something simple.
NetworkCredential netcreds=new NetworkCredential(username,password);
CredentialCache credentials = new CredentialCache();
credentials.Add(new Uri(LoginUrl.ToString()),"Basic",netcreds);
Uri LoginUri = new Uri(LoginUrl);
WebRequest AuthRequest = WebRequest.Create(LoginUri);
AuthRequest.Credentials = credentials; `
AuthRequest.Headers.Add(HttpRequestHeader.AcceptEncoding,#"application/*+xml;version=1.5");`
I want to request reports from a third party and they require "Basic Access Authentication" via POST:
Your client application must use Basic Access Authentication
to send the user name and password.
Can someone point me in the right direction?
Edit: I did see this post but there are two answers and I'm not sure if thats what I need to do or which one is the preferred method.
Assuming you use a WebRequest, you attach a CredentialCache to your request:
NetworkCredential nc = new NetworkCredential("user", "password");
CredentialCache cc = new CredentialCache();
cc.Add("www.site.com", 443, "Basic", nc);
WebRequest request = WebRequest.Create("https://www.site.com");
request.Credentials = cc;
request.PreAuthenticate = true;
request.Method = "POST";
// fill in other request properties here, like content
WebResponse respose = request.GetResponse();
The basic gist is like this:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Credentials = new NetworkCredential(username, password);
but sometimes there are issues with using request credentials, the alternative is add the authentication data in request headers
string authInfo = username + ":" + password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
for more details see this blog post
http://charlie.cu.cc/2012/05/how-use-basic-http-authentication-c-web-request/