Response.Redirect () in iFrame, redirect parent window

I know this is not possible, but what is the best alternative for those wishing to make Response.Redirect from iFrame to redirect the parent page?

+6
source share
4 answers

You cannot do this with ASP.NET. ASP.NET on the server side can redirect the incoming request and cannot know about the parent frame.

But if you want to redirect the parent frame to some condition on the server side, you can call JavaScript from the server as follows:

 protected void Page_Load(object sender, EventArgs e) { ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "RedirectScript", "window.parent.location = 'http://yoursite.com'", true); } 

And of course, you can use simple JavaScript window.parent.location = 'http://yoursite.com' on the client side.

+11
source

I just used the following code with success. It even circumvented X-Frame-Options SAMEORIGIN and allows you to redirect from one domain to another in an iframe:

 string url = "https://siteurl.com"; Response.Write("<script>top.location='"+url+"';parent.location='"+url+"';</script>"); 

With string interpolation (starting with C # 6):

 string url = "https://siteurl.com"; Response.Write($"<script>top.location='{url}';parent.location='{url}';</script>"); 
+6
source
 Response.Clear(); Header.Controls.Add(new LiteralControl(@" <script type=""text/javascript""> top.location = ""/Logout.aspx""; parent.location = ""/Logout.aspx""; </script> ")); 
+2
source
  url = "Your.asp?YourVar="&YourVar %> \end your classic asp code and pass to the script below <script type='text/javascript'>window.top.location.href = '<%= url %>'</scrip> 
0
source

Source: https://habr.com/ru/post/896634/


All Articles