Avoid using user name and password for HttpClientHandler - c#

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

Related

Basic authentication requires a secure connection to the server. in TFS

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);

Read email form Exchange using C# EWS

im trying to read email from exchange server using the below script :
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ProhibitDtd = false;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidationCallBack);
service.Credentials = new WebCredentials("user", "password" , "domain");
service.UseDefaultCredentials = true;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.Url = new Uri("https://IP/");
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
Label1.Text = inbox.DisplayName.ToString();`
but im getting this error :
DTD is prohibited in this XML document.
i try this solution :
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ProhibitDtd = false;
but error stil there .
XmlReaderSettings xrs = new XmlReaderSettings()
xrs.DtdProcessing = DtdProcessing.Parse;
Documented at: https://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.dtdprocessing(v=vs.110).aspx

default system proxy settings

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.

Network Credentials not saving in CredentialCache

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

HttpClient with Authentication returns 404. Why?

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);

Categories

Resources