redirecting to page from inside iframe - c#

I have just now started coding in .NET framework, so my apologies if this issue happens to be of trivial nature.
What I got now
A main.aspx page with simple layout using three iframes
the middle iframe content needs to be dynamic (first a login.aspx page and after logging entryform.aspx)
Issue #1 :
After logging in login.aspx inside the iframe, redirecting to main.aspx
The solution I found:
ClientScript.RegisterStartupScript(this.GetType(),"scriptid",
"window.parent.location.href='main.aspx'", true);
(http://forums.asp.net/t/1273497.aspx)
Issue #2
After redirecting/logging how do I change the middle iframe content from login.aspx to entryform.aspx?
The silly solution I thought of:
Add '#form' to the url and listen to hashchange event in main.aspx. But then, anyone can get to the form using the url itself.
So, basically how do I find a secure way to tell the main.aspx page that it needs to change it's middle iframe content after the redirecting/logging
Or by any chance there is a request.setAttribute and getAttribute in .NET like in java that I have missed and made things difficult for me?

Passing variables or values across pages and domains wont be issue, you can use post method and cross page posting for that

After finding that the use of iframe isn't exactly the best idea in my case, I took Tieson T's advice and looked into HttpClient to fetch content from other web pages. In my case it will be both from same domain and other domains.
Since I have 4.0v .NET instead of HttpClient I used HttpWebRequest
code
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (http://localhost:1706/WebSite3/test.html);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader(response.GetResponseStream(),encode );
string html= reader.ReadToEnd();
myDiv.innerHtml(html);
References
HttpClient does not exist in .net 4.0: what can I do?
http://msdn.microsoft.com/en-us/library/456dfw4f.aspx
http://forums.asp.net/t/1382935.aspx/1
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx
http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx

Wouldn't it be easier to just pass a parameter to Main.aspx? E.g.
ClientScript.RegisterStartupScript(this.GetType(),"scriptid", "window.parent.location.href='main.aspx?LoadEntry=true'", true);
And client-side JavaScript code inside of 'main.aspx' would read that parameter from 'location.search' and if 'LoadEntry=true' is set - would set SRC of middle frame to "entryform.aspx".
Of course inside of "entryform.aspx.cs" you would need to check if correct login really took place (e.g. some Session variable is set) so nobody would be able to simple set URL manually to "main.aspx?LoadEntry=true" to bypass the login.

Related

HttpWebResponse returns only one element of the page

Hello i making a simple httpwebrequest and then i read (StreamReader) the response and just want to get the html page of website,but i get only one laber(only one element of the page) in the browser all fine(i see all page) but when i try to set cookies to Deny\disable i also in the browser get this label(only one element of the page) and all is disappear.Sow i getting opinion if after i disabled cookies in browser i get the same page(like in code) that mean my HttpWebRequest is have settings cookies=deny/disable.
You can go to https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html and disable cookies with F12 and you will see the difrance and also this page with one label.
Sow this my code any ideas what i need to change here?
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html");
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream streamResponseLogin = myHttpWebResponse.GetResponseStream();
StreamReader streamReadLogin = new StreamReader(streamResponseLogin);
LoginInfo = streamReadLogin.ReadToEnd();
Your code is receiving complete page content, but it cannot receive the dynamic contents. This is happening because the page you are trying to access relies on Cookies for maintaining session as well as JavaScript (it is using jQuery) for loading dynamic contents and providing rich user experience.
To successfully receive the whole page, your code must
support retrieving, storing and sending cookie objects across various HttpRequest and HttpResponse.
be able to execute JavaScript code to load the dynamic contents/markup of the page
To test 'if your code is receiving proper values or not' visit the site Web Sniffer and put your URL there.
As you can try on web-sniffer site, for www.google.com, the response you are getting is a redirect instruction.... that means, even to access the Google's home page, your code must understand HTTP status messages (302 there).

asp.net button onclick post to another page

I have a page in .Net that currently does some processing of info when a button is clicked. It posts back, updates some info and then redirects the user onwards.
What I want to do now is for this same button, when it's clicked, the info is updated but instead of a redirect it does a POST to another site. The reason being that this other site needs to read a bunch of data from the form I submit.
So, I know about the PostBackUrl property but that will stop the processing of the data that I need done.
So is there another way for me to be able to somehow combine both a postback that then becomes as POST to another site?
Or alternatively some way for me to be able to do the updates I need and then do a POST?
The suggested solutions all kind-of worked but the only one that actually did exactly what I needed it to was this one:
Link to SO answer
The reason the other answers above didn't work is that I was POSTing to a payment gateway and for whatever reason their system thought there was a problem with various missing fields in all solutions except the one I linked to. No idea why, I don't have access to their systems to know what they were actually doing.
In any case, thanks for all answers but have a look at the linked one if you're running into a similar issue.
if PostBack isn't absolutely needed, you can actually send them in the request query itself.
You can do a POST from codebehind, you can find details in this answer Redirect to another page using Post method from Code behind
If I got your question right I think you will need to get all the form data from Request.Form and make a HttpWebRequest to the other site:
string url = "http://anothersite.com/";
// create post data
StringBuilder postDataBuilder = new StringBuilder();
foreach (var key in this.Request.Form.AllKeys)
{
postDataBuilder.AppendFormat("{0}={1}&", this.Request.Form[key]);
}
string postData = postDataBuilder.ToString();
// create the web request for the POST
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
// add the post data
using (StreamWriter requestStream = new StreamWriter(webRequest.GetRequestStream()))
{
requestStream.Write(postData);
}
Hope this helps!

An image calls an ASP utility. How do I do that without image in ASP.NET (C#)

I have a store (C#) that calls our "back office(ASP classic)" via a one-pixel image with some parameters to record a sale. It looks like this:
<asp:Image ID="BOImageLink" runat="server" ImageUrl="https://backoffice.mysite.com/Import.asp?TicketType=Import"></asp:Image>
The store's code-behind appends other parameters to the ImageUrl as needed.
About once per day someone gets through the system without triggering the image tag link. My task is to make the call to that script from our store which is ASP.NET C# code without using an image tag and I just don't know how to do it. All help appreciated, answering accordingly knowing I am new to .NET is doubly so.
That's a really random way of doing that!
Why don't you do a web request in code instead?
http://msdn.microsoft.com/en-us/library/5t9y35bd.aspx
Would be something like this:
WebRequest request = WebRequest.Create ("https://backoffice.mysite.com/Import.asp?TicketType=Import");
using this to get back any response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

How to capture HTML of redirect page before it redirects?

I am trying to read the HTML of a page that contains a non-delayed redirect. The following snippet (C#) will give me the destination/redirected page, not the initial one I need to see:
using System.Net;
using System.Text;
public class SomeClass {
public static void Main() {
byte[] data = new WebClient().DownloadData("http://SomeUrl.com");
System.Console.WriteLine(Encoding.ASCII.GetString(data));
}
}
Is there a way to get the HTML of a redirecting page? (I prefer .NET but a snippet in Java or Python would be fine too. Thx!)
Unless the redirect is done on the client side you can't. If the redirect is done server side, then no html is actually generated to the client, but the header is redirected at the new server.
It would take more work, but rather than using WebClient, use HttpWebRequest and set the AllowAutoRedirect property to False. A redirect will then throw an exception, but you can get any response text (and some pages do have response text along with the redirect) from the exception's response object. After you get the response from the exception, you can issue another HttpWebRequest for the redirect URL (specified in the Location response header).
You might be able to do something similar with WebRequest if you create a derived object, MyWebRequest, where you overload the GetWebRequest method and set the AllowAutoRedirect property. I don't know what kind of exception, if any, the DownloadData method will return if you do something like that.
As somebody said previously, this will only work for those pages that do client-side redirects (typically 301 or 302). If there is server-side redirection going on, you'd never know it.
Simplest answer would be to add the current page onto the QueryString component of the redirect when redirecting, for instance:
Response.Redirect(newPage + "?FromPage=" + Request.Url);
Then the new page could see where you cane from by simply looking at Request.QueryString("FromPage").
If you want to get the source of an html page you can use this tool:
http://www.selfseo.com/html_source_view.php

Server side include external HTML?

In my asp.net-mvc application I need to include a page that shows a legacy page.
The body of this page is created by calling an existing Perl script.
This Perl script is externally hosted.
Is there a way to do something like this:
<!-- #Include virtual="http://www.example.com/theScript.plx"-->
Not as a direct include, because ASP.NET server-side-includes require the page to be compiled at the server.
You could use jQuery to download the HTML from that URL when the page loads, though I appreciate that's not perfect.
Alternatively (and I have no idea whether this will work) you could perform a WebRequest to the perl webpage from your ASP.NET MVC controller, and put the resulting HTML in the view as text. That way you could make use of things like output caching to limit the hits to the perl page if it doesn't change often.
If you wanted to do it all in one go, you could do an HTTP Request from the server and write the contents to the page?
Something like this:
Response.Write(GetHtmlPage("http://www.example.com/theScript.plx"));
Calling this method:
public String GetHtmlPage(string strURL)
{
// the html retrieved from the page
String strResult;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
objResponse = objRequest.GetResponse();
// the using keyword will automatically dispose the object
// once complete
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return strResult;
}
(Most code ripped blatantly from here and therefore not checked)
You could implement this in a low-key fashion by simply using a frame and setting the frame source to the url that needs to be included. This is quite simple and can be down without any server or client side scripting, so that'd be my preferred approach, if possible.
If you want the html to appear to come from your server, however, you'll need to manually include it - typically by using WebRequest as Neil says. You may wish to cache the remote page for performance, though, since it's a perl script, I'll assume the page is dynamic, so this might not be a great idea.

Categories