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
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 have local tfs 2012. On Visual studio 2012 , use c# , I write program which programmatically connect to tfs server. But I have error:
{"TF30063: You are not authorized to access http://server:8080/tfs."} System.Exception {Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException}
My code:
Uri collectionUri = new Uri("http://server:8080/tfs");
NetworkCredential netCred = new NetworkCredential("login","password");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();//throw error here
Can you help me fix this error?
P.S. I try do connect this way, but I have same error:
var projectCollection = new TfsTeamProjectCollection(
new Uri("http://myserver:8080/tfs/DefaultCollection"),
new NetworkCredential("youruser", "yourpassword"));
projectCollection.Connect(ConnectOptions.IncludeServices);
And this way:
Uri collectionUri = new Uri("http://server:8080/tfs/DefaultCollection");
NetworkCredential netCred = new NetworkCredential("login","password","Server.local");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();
Hope it helps you.
var tfsCon = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://server:8080/tfs"));
tfsCon.Authenticate();
var workItems = new WorkItemStore(tfsCon);
var projectsList = (from Project p in workItems.Projects select p.Name).ToList();
or
Uri TfsURL = new Uri(""http://server:8080/tfs"");
NetworkCredential credential = new NetworkCredential(Username, Password, Domain);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(TfsURL, credential);
collection.EnsureAuthenticated();
If fails here you need to configure in App.config to set the default proxy:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>
</configuration>
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 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);