I have an API that needs authentication with username and password. I can't figure out why my code fails and the response is 404, when I'm doing a GET method and give the actual credentials:
NetworkCredential networkCredentials = new System.Net.NetworkCredential("adminUN", "mypassword");
HttpClientHandler handler = new HttpClientHandler();
handler.PreAuthenticate = true;
handler.Credentials = networkCredentials;
httpClient = new HttpClient(handler);
HttpResponseMessage response = await httpClient.GetAsync(GlobalDeclarations.strURL);
Related
I am using the following code to prepare a handler for HttpClient:
var uri = new Uri("URL");
var credentialsCache = new CredentialCache();
credentialsCache.Add(uri, "NTLM", new NetworkCredential("username", "password"));
var handler = new HttpClientHandler() { Credentials = credentialsCache, PreAuthenticate = true };
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
};
....
new HttpClient(handler);
Is it possible to avoid using real username and password?
Thanks
I was try to connect my TFS server using my credentials . But i am getting error 'Basic authentication requires a secure connection to the server.'
string username = "adminuser";
string pwd = "mypassword";
string domain = "http://localhost:8080/tfs/defaultcollection";
NetworkCredential networkCredential = new NetworkCredential(username, pwd);
BasicAuthCredential basicAuthCredential = new BasicAuthCredential(networkCredential);
TfsClientCredentials tfsClientCredentials = new TfsClientCredentials(basicAuthCredential)
{
AllowInteractive = false
};
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(domain), tfsClientCredentials);
tfs.EnsureAuthenticated();
My tfs didn't have the https. Any alternative to fix it But browser level it is working fine
The BasicAuthCredential requires https://, I believe, and I wasn't able to access my TFS with https://. So I found another way to get from NetworkCredential to VssCredentials.
string username = "adminuser";
string pwd = "mypassword";
string domain = "http://localhost:8080/tfs/defaultcollection";
NetworkCredential networkCredential = new NetworkCredential(username, pwd);
//BasicAuthCredential basicAuthCredential = new BasicAuthCredential(networkCredential);
Microsoft.VisualStudio.Services.Common.WindowsCredential winCred = new Microsoft.VisualStudio.Services.Common.WindowsCredential(networkCredential);
VssCredentials vssCred = new VssClientCredentials(winCred);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(domain), vssCred);
tfs.EnsureAuthenticated();
Try using the following code:
String collectionUri = "http://localhost:8080/tfs/defaultcollection";
VssCredentials creds = new VssClientCredentials();
creds.Storage = new VssClientCredentialStorage();
VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
I'm going to sent SOAP-request to the host via proxy. I use two variants: manual proxy settings and system proxy settings. There is my code:
if (UseProxy.Equals("manual"))
{
WebProxy p = new WebProxy(pServer, true)
{
Credentials = new NetworkCredential(ProxyUser, ProxyPass)
};
WebRequest.DefaultWebProxy = p;
Request.Proxy = p;
Request.Timeout = 10000;
}
else if(UseProxy.Equals("system"))
{
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
Request.Proxy = proxy;
Request.Timeout = 10000;
}
Manual settings are working well, but system proxy settings don't work at all. I can not send anything and program hangs out on request regardless of the timeout parameter.
Also I tried the following:
WebProxy proxy = new WebProxy(ProxyHost, 8080);
proxy.UseDefaultCredentials = true;
Request.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = proxy;
Request.Proxy = proxy;
Request.Timeout = 10000;
It didn't work too. Please explain me where is my error? Class with SOAP-requests was generated authomatically using wsdl-file.
I saw one artcle how to share our status in LinkedIn using c#.
I follow those steps and the code I am using is following...
For this I am using Hammock library.
OAuthCredentials credentials = new OAuthCredentials();
credentials.Type = OAuthType.AccessToken;
credentials.ConsumerKey = "****";
credentials.ConsumerSecret = "*****";
credentials.Token = "******";
credentials.TokenSecret = "*********";
credentials.SignatureMethod = OAuthSignatureMethod.HmacSha1;
credentials.ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader;
RestClient client = new RestClient();
client.Authority = "http://api.linkedin.com/v1";
client.Credentials = credentials;
client.Method = WebMethod.Put;
byte[] msg = Encoding.Default.GetBytes("Test");
client.AddHeader("Content-Type", "text/xml");
client.AddPostContent(msg);
RestRequest request = new RestRequest();
request.Path = "http://api.linkedin.com/v1/people/~/current-status";
/* ERROR */ RestResponse response = client.Request(request);
It's giving me a error "Bad Request."
How to solve this please help me..?
I have to assign the username and password from credential cache to Httpwebrequest.When i assign to cache it is working but when assigning to Webrequest from cache it shows null. Where i'm wrong?
string strServer = "";
strServer = "http://servername/";
Uri uri = new Uri(strServer);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(strServer), "Basic", new System.Net.NetworkCredential("username", "password","Domain"));
cache.Add(new Uri(strServer), "Digest", new System.Net.NetworkCredential("username", "password","Domain"));
request.Credentials = cache;
Hello,
You just need to retrieve the credential from the cache, and then add it to the web request, example bellow:
cache.Add(new Uri(strServer), "Basic", new System.Net.NetworkCredential("username", "password","Domain"));
cache.Add(new Uri(strServer), "Digest", new System.Net.NetworkCredential("username", "password","Domain"));
NetworkCredential currentCredentials= cache.GetCredential(new Uri(strServer), "Basic"); //Get specific credential
request.Credentials = currentCredentials; //Allocate credential