I am trying to get the cookie from the wordpress website programatically and use the asp.net website to store the cookie on the browser.
What i wanted to happen is that, the cookie I got will be stored in the browser so that when I browse that page in the wordpress website, the login page will not be shown.
Is there a problem with the code below?
var url = "https://app.myDomain.com/bfSignin.php";
var cookies = new CookieContainer();
var request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var data = "passWord=asdf1234&userName=premium";
var buffer = Encoding.ASCII.GetBytes(data);
using (var stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
var response = (HttpWebResponse)request.GetResponse();
response.Cookies.Add(cookies.GetCookies(new Uri(url)));
response.Close();
var cookie = cookies.GetCookies(new Uri(url))[0];
var newCoookie = new HttpCookie(cookie.Name, cookie.Value);
newCoookie.HttpOnly = true;
newCoookie.Path = "/test/";
this.Response.Cookies.Add(newCoookie);
return View();
here is the screenshot if the http response header
Related
Trying to fetch data from a URL and then parse the sensitive content.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(resp.GetResponseStream());
var data = reader.ReadToEnd();
If check the data, not everything got returned. How can I fetch the dynamic parts which I can see on the page like Chrome Dev tool does from browser.
I am currently using EO browser and trying to set up cookies after my login. I have manually tried to set the cookies to the EO browser ( connect-sid cookie) and it worked. But when I make a HttpWebRequest I can not see cookie information in response headers or anywhere in response. I am using Express and passport js in the login authentication.
CookieContainer cookieContainer = new CookieContainer();
string param = "username=test#yahoo.com&password=123456";
string url = "http://localhost:8080/login";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = param.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
using (Stream stream = request.GetRequestStream())
{
byte[] paramAsBytes = Encoding.Default.GetBytes(param);
stream.Write(paramAsBytes, 0, paramAsBytes.Count());
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
MessageBox.Show(response.Headers.ToString());
MessageBox.Show(response.SupportsHeaders.ToString());
MessageBox.Show(response.GetResponseHeader("Response Headers"));
foreach (var cookie in response.Headers)
{
var properties = cookie.GetType()
.GetProperties()
.Select(p => new
{
Name = p.Name,
Value = p.GetValue(cookie)
});
foreach (var property in properties)
{
MessageBox.Show(property.Value.ToString());
}
}
This is the result I get
I would like to see or get this value
Ok, as always right after I post my question I have found the solution - > I was doing redirect after login. so when I remove it I could able to see the value.
Though, any idea how can I get the value of SetCookie?
my question is, how can I call multiple requests via HttpWebRequest with same authenticate cookie in C#? I tried a lot of times but for now I dunno how to do it :/
My code is below:
var postData = "method=loginFormAccount&args[0][email]=###&args[0][pass]=###&args[0][cache]=37317&args[]=1";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("###");
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var cookies = new CookieContainer();
cookies.Add(response.Cookies);
System.IO.File.WriteAllText(#desktop + "\\post.html", new StreamReader(response.GetResponseStream()).ReadToEnd());
// =================================== END LOGIN ==================================== \\
System.IO.File.WriteAllText(#desktop + "\\cookie.html","");
foreach (Cookie cook in response.Cookies)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#desktop + "\\cookie.html", true))
{
file.WriteLine(cook.ToString());
}
// Show the string representation of the cookie.
}
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer = cookies;
requestNext.Method = "GET";
HttpWebResponse responseNext = (HttpWebResponse)requestNext.GetResponse();
//var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
System.IO.File.WriteAllText(#desktop + "\\get.html", new StreamReader(responseNext.GetResponseStream()).ReadToEnd());
My main problem is that, cookie which I'm getting is the cookie BEFORE authenticate so I must to do something to get cookie AFTER authenticate.
Try this :
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer.Add(cookies);
I have a web service URL which has username and password authentication mode. I have to first pass the username and password, and if I am authenticated, I can upload a text or XML file onto the server. I am looking for a C# code to do the same process, but I'm unable to find it.
Any suggestions would be highly appreciated.
I am using following code-
if (!string.IsNullOrEmpty(txtfile))
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.KeepAlive = false;
request.SendChunked = true;
request.AllowAutoRedirect = true;
request.Method = "Post";
request.ContentType = "text/xml";
request.Credentials = new NetworkCredential(userName, password);
var encoder = new UTF8Encoding();
var data = encoder.GetBytes(txtfile);
request.ContentLength = data.Length;
var reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
WebResponse response = null;
response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
}
You might want to try using the WebClient Class. There is a simple example about the WebClient.UploadFile Method which could fit your scenario.
I'm trying to write a bit of code to login to a website. But it's not working. Please can you give me some advice. This is my a bit of code:
static void Main(string[] args)
{
CookieContainer container = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://pagehype.com/login.php");
request.Method = "POST";
request.Timeout = 10000;
request.ReadWriteTimeout = 30000;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)";
request.CookieContainer = container;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user&password=password&processlogin=1&return=";
byte[] data = encoding.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string htmldoc = reader.ReadToEnd();
response.Close();
Console.Write(htmldoc);
}
Many thanks,
Use http://www.fiddler2.com/fiddler2/ to view the http request sent went you login in using a browser and ensure that the request you build in code is the same.
PHP logins use PHPSESSID cookie. You'll need to capture this and pass it back in the CookieContainer. This is how the server will recognise you as an authenticated user.
The cookie is set in the Set-Cookie header in the initial response. You'll need to parse it to recreate the cookie in your container (don't forget the path (and domain?)
var setCookie = response.GetResponseHeader("Set-Cookie");
response.Close();
container = new CookieContainer();
foreach (var cookie in setCookie.Split(','))
{
var split = cookie.Split(';');
string name = split[0].Split('=')[0];
string value = split[0].Split('=')[1];
var c = new Cookie(name, value);
if (cookie.Contains(" Domain="))
c.Domain = split.Where(x => x.StartsWith(" Domain")).First().Split('=')[1];
else
{
c.Domain = ".pagehype.com";
}
if (cookie.Contains(" Path="))
c.Path = split.Where(x => x.StartsWith(" Path")).First().Split('=')[1];
container.Add(c);
}
Then add this container to your request.