network Authentication and website Authentication using HttpWebRequest - c#

I am trying to create a application that will consume RSS data using .NET Framework. The RSS site requires User name and Password to start with.
and Am running this application from within my work place which requires NTLM authentication to connect to internet.
Following is the code that i am trying to use
NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = cache;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
i get 407 error and if i simply use CredentialCache.DefaultNetworkCredentials i get 401 error.

solved the issue by using following code
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password"));
StringBuilder outputData = new StringBuilder();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
myHttpWebRequest.Headers.Add("Authorization", "Basic " + credentials);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream streamResponse = myHttpWebResponse.GetResponseStream();

If this code works, then your original code above was wrong. You should set
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
and
NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Credentials = cache;
In other words, you had exchanged credentials for the proxy and the destination server.

Related

Using C# HttpWebRequest with not typical URL

I am using the following function to receive an Artifacory repo mapping:
private string LoadHttpPageWithBasicAuthentication(string url, string username, string password)
{
Uri myUri = new Uri(url);
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream responseStream = myWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
responseStream.Close();
myWebResponse.Close();
return pageContent;
}
The URL that i am trying to pass looks like that -
http://ArtifactoryRepo?list&deep=1&listFolders=1&mdTimestamps=1
But from the result it seems that the request ignores part of the URL.
The results that received are only for that part of the URL -
http://ArtifactoryRepo
I tried to split URL for two separate parameters but it doesn't worked
Any ideas ?
Thnaks
First there was a problem with credentials, so i used Convert.Tobase64string() function.
And webReq.Method was not defined.
Finally found that function to do the job.

Login to site using watin - c#

The test site I have displays a pop up box to enter user credentials. I can log in to that site using
private CredentialCache GetCredential(LoginData loginData)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(loginData.LoginURL), "Basic", new NetworkCredential(loginData.LoginUserName, loginData.LoginPwd));
return credentialCache;
}
//above code is thanks to another post on SO
public void SiteLogin(LoginData loginData)
{
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = WebRequest.Create(loginData.LoginURL) as HttpWebRequest;
request.CookieContainer = cookieContainer;
request.Method = WebRequestMethods.Http.Post;
request.KeepAlive = false;
request.ContentLength = 0;
request.Credentials = GetCredential(loginData);
request.PreAuthenticate = true;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string testString = reader.ReadToEnd(); //see what response is here, only for initial coding
}
I do not have access to the test web site so I do not know what the names of the controls are for the login pop up.
How do i perform similar step with watin?

Using proxy with WebBrowser and WebRequest, how to include username and password?

I got this from Load web browser with web response. and am wondering how I can use this code to use proxies that need a username and password to be able to work.
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://example.com");
webRequest.Proxy = new WebProxy(host, port);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream receiveStream = response.GetResponseStream();
WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentStream = receiveStream;
var webProxy = new WebProxy(host,port);
webProxy.Credentials = new NetworkCredential("username", "password", "domain");
var webRequest = (HttpWebRequest)WebRequest.Create("http://example.com");
webRequest.Proxy = webProxy;

The requested URI is invalid for this FTP command

I have added the following code in my C#.net application in visual studio 2010
WebRequest request = WebRequest.Create("ftp://myftp.com");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
But I am getting following error.
The requested URI is invalid for this FTP command.
Please suggest solution. Thanks
If you just want to check file information you should try changing the request.Method to WebRequestMethods.Ftp.ListDirectoryDetails instead:
WebRequest request = WebRequest.Create("ftp://myftp.com");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
Uri should be ftp:// + IP or ftpServerName + / Folder_to_create_Name
So, see follow code:
WebRequest request = WebRequest.Create("ftp://myftp.com/FOLDER_TO_CREATE/");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
You must specify a sub-directory you wish to create; you can't create the root, which is what it appears you are trying to do. Hence the error.

How to pass credentials to httpwebrequest for accessing SharePoint Library

I'm trying to read files from a SharePoint document library using HttpWebRequest. In order to do that I have to pass some credentials. I'm using the below request:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/msexcel";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
request.Credentials = new NetworkCredential(UserName, PassWord);
Is this the correct way to pass credentials?
You could also use:
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
If you need to run request as the current user from desktop application use CredentialCache.DefaultCredentials (see on MSDN).
Your code looks fine if you need to run a request from server side code or under a different user.
Please note that you should be careful when storing passwords - consider using the SecureString version of the constructor.
If you need to set the credentials on the fly, have a look at this source:
http://spc3.codeplex.com/SourceControl/changeset/view/57957#1015709
private ICredentials BuildCredentials(string siteurl, string username, string password, string authtype) {
NetworkCredential cred;
if (username.Contains(#"\")) {
string domain = username.Substring(0, username.IndexOf(#"\"));
username = username.Substring(username.IndexOf(#"\") + 1);
cred = new System.Net.NetworkCredential(username, password, domain);
} else {
cred = new System.Net.NetworkCredential(username, password);
}
CredentialCache cache = new CredentialCache();
if (authtype.Contains(":")) {
authtype = authtype.Substring(authtype.IndexOf(":") + 1); //remove the TMG: prefix
}
cache.Add(new Uri(siteurl), authtype, cred);
return cache;
}

Categories

Resources