Frame Redirect in C# - c#

I would like to execute a frame redirect in C# from my managed module for the IIS 7.
When I call context.Response.Redirect(#"http://www.myRedirect.org");the correct page is shown but also the address is shown in the browser. And that is exactly what I do not want.
So I want something like:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// make a frame redirect if a specified page is called
if (context.Request.ServerVariable["HTTP_REFERER"].Equals(#"http://www.myPage.org/1.html"))
{
// perform the frame redirect here, but how?
// so something like
context.Response.Redirect(#"http://www.myRedirect.org");
// but as I said that doesn't redirect as I want it to be
}
}
Any ideas about that?
EDIT:
I tried the example, so I have:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// make a frame redirect if a specified page is called
if (context.Request.ServerVariable["HTTP_REFERER"].Equals(#"http://www.myPage.org/1.html"))
{
// perform the frame redirect here, but how?
context.Response.Write(#"<html>");
context.Response.Write(#"<head>");
context.Response.Write(#"</head>");
context.Response.Write(#"<frameset rows=""100%,*"" framespacing=""0"" frameborder=""NO"" border=""0"">");
context.Response.Write(#"<frame src=""http://www.myRedirect.org"" scrolling=""auto"">");
context.Response.Write(#"</frameset>");
context.Response.Write(#"<noframes>");
context.Response.Write(#"<body>Some text...");
context.Response.Write(#"</body>");
context.Response.Write(#"</noframes>");
context.Response.Write(#"</html>");
}
}
But that also doesn't correctly redirect. I still have the redirect address shown in my browser. So any other idea?
EDIT:
I obviously made a mistake. The code above works and does what I want. It first didn't work because my redirect url was doing something unexpected.

To perform a frame redirect you need to send back the HTML code containing a frameset with a single frame, with it's source set to http://www.myRedirect.org. As far as the server and browser is concerned no redirect has happened - it's just received some HTML code.
Performing a Response.Redirect will, as you've observed, cause the browser to make a fresh new request to the new page, showing the user the new address in the title bar. It's typically used for when a page actually changes its address, but the owners still want it reachable from the original URL as well.
EDIT: HTML frame redirect sample: http://en.wikipedia.org/wiki/URL_redirection#Frame_redirects

Related

Close webview xamarin forms

I have implement payment method with return URL and using webview open URL with my app. Recent, I'am issue want auto close webview with response success or fail process payment.
I encountered this case. Here is the solution I found.
After creating the WebElement, we bind the Navigating event to a certain method.
MyPage.Source = link;
MyPage.Navigating += Webview_Navigating;
MyPage WebElment name.
link is my link address
.
Then we perform our transactions here.
private void Webview_Navigating(object sender, WebNavigatingEventArgs e)
{
var url = e.Url;
if (url.Contains("status=1"))
{
int ordID = 0;
//save order
}
}
For me, if the changed link address checkout status=1, the checkout is successful.
You need to call Url property in WebNavigatingEventArgs to catch the changed url
If useful, choose. Good Luck
Sure! You can get response form WebView.
I recommend you use HybridWebView (see: https://github.com/devsmadeofsteel/Plugin.HybridWebView) as alternative.
Examples:
In Xamarin page, create a browser add register a named callback, and add browser to page childs
HybridWebViewControl Browser = new HybridWebViewControl();
//You can load browser content by string or URL, here is string which you can hard code or store in resource files
Browser.ContentType = Plugin.HybridWebView.Shared.Enumerations.WebViewContentType.Internet;
Browser.Source = YourUrl;
Browser.AddLocalCallback(YourCallBackFunctionName, CallBackFunction);
When run, HybridWebViewControl will insert a function with the name you specified in
YourCallBackFunctionName, and the single parameter is string type. So, In JavaScript of the page loaded by YourUrl, you can call back with the specified CallBackFunctionName:
function AnyFunction() {
//...
YourCallBackFunctionName(YourStringParameter);
}
When you call YourCallBackFunctionName(YourStringParameter) in JavaScript, you can get YourStringParameter in CallBackFunction using C#.
So, you could define different CallBackFunctions or pass different parameters with single CallBackFunction, to control the HybridWebView and the page or view which contain it, such as hide HybridWebView or close the page.
Have a try!

MVC 4 Lose Session Because Redirects From www.website.com to website.com

I just realize that session is lost when redirecting from www.website.com to website.com without www.
However I am quite lucky that I can set up the server so all redirection is start with www. In the future I do not want my application to be server dependent, I want to make it code dependent.
For example:
#Html.Hidden("PageManagerUrl", Url.Action("PageManager","Admin"))
produces /Admin/PageManager/, and I use location.href = url (when url is /Admin/PageManager/.
It is not 100% sure that I will lose my session at the first time of redirection, but when I tested it but redirecting back and forward, about the 3rd or 4th time, the session will be lost. After I set up the server setting to:
Preferred domain * www.website.com
Select the URL (either with or without the www. prefix) to which site visitors will be redirected via a SEO-safe HTTP 301 redirect.`
The session is never lost again. So I wonder, how to do the best redirection without losing my session? Please give me example how to do it from controllers and javascript / views.
Do you have any global.asax file ?
If yes, then please check whether it contains these two methods signature.
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}

Spotify authorization code flow (WPF browser control) C#

I'm using Spotify account service (authorization code flow) to get an authorization code by passing in the required parameters (client_id, response_type and redirect_uri. Its a WPF application so i am using the browser control and navigating the user to
https://accounts.spotify.com/authorize/?client_id=myclientId&response_type=code&redirect_uri=someUri
When i copy paste the link in the browser, i see the right stuff i.e page with the Login to Spotify button but when i am navigating through the browser control in my WPF application, it gives me a file download dialog with Authorize.json file to download with Open and Save options, however in some cases it presents the right page to browser code.
Below is my code:
public winOAuthBrowserForm(string navigateTo)
{
InitializeComponent();
webBrowser.Navigated += webBrowser_Navigated;
webBrowser.Navigate(navigateTo);
}
private void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if(!String.IsNullOrEmpty(e.Uri.Query))
{
//since we are looking for code for authorization that will be exchanged for request token from the server
if (e.Uri.Query.StartsWith("?code=") || e.Uri.Query.Contains("code="))
{
code = HttpUtility.ParseQueryString(e.Uri.Query).Get("code");
this.Close();
}
if (e.Uri.Query.StartsWith("?error=") || e.Uri.Query.Contains("error="))
{
error = HttpUtility.ParseQueryString(e.Uri.Query).Get("error");
this.Close();
}
}
}
And i am calling this control like:
var uri = string.Format(SpotifyAuthUriFormatter, RequestAuthBaseUrl, clientId);
winOAuthBrowserForm form = new winOAuthBrowserForm(uri);
form.ShowDialog();
This is what i see:
UPDATE: When i right click on document and see the properties, i see it changes the URL to something like res://ieframe.dll/navcancl.htm#... I've searched it and found some solutions related to I.E (I'm using latest but you can't be sure that it'll be latest on client machines), some say that its firewall setting. The thing is, it appears sometimes only.
Any ideas? Thanks

ASP.Net on start runs wrong page as Sartup Page

I have a simple folder structure for my multilingual website on localhost
Default.aspx
images
css
js
en/Default.aspx
en/ContactUs.aspx
....
ar/Default.aspx
ar/xxxxx.aspx
Problem i am facing is very strange to me. i have a simple code to check the browser language set by user and accordingly i redirect user to English or Arabic version of website.
Irrecpective of what code i wring it always redirects me to English version of website and executes the en/Default.aspx page
Even commenting all the code in Default.aspx page it still redirect it to the en/Default.aspx page. while it should not do any thing.
I have set Default.aspx as Set As Default Page but it doesn't make any difference. I removed even global.asa that had routing code, i also removed all the compiler code related to this website on local host but it still keeps doing the same thing.
I have checked web.config file there is nothing wrong with that.
Even after removing the Default.aspx page it sill redirects me to en/Default.aspx i am frustrated with this problem.
I am not sure what is wrong. I re-started system with no result.
I am using visual studio 2010 for asp.net web form project.
http://localhost:49831/AlShindagah/
ALWAYS take me to below URL
http://localhost:49831/AlShindagah/en/Default.aspx
CODE of Default.aspx before i deleted it
public partial class DefaultMain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//switch (Session["lang"].ToString().ToLower())
//{
// case "en-us":
// Response.RedirectPermanent("~/en/Default.aspx");
// break;
// case "ar-ae":
// Response.RedirectPermanent("~/ar/Default.aspx");
// break;
// default:
// Response.RedirectPermanent("~/en/Default.aspx");
// break;
//}
}
//// Localization and Globalization code
//protected override void InitializeCulture()
//{
// String lang = Request["Language"];
// Session["lang"] = Helper.DetectLanguage(lang);
// //Set Direction of page LTR/RTL
// if (Session["lang"] == "ar-AE")
// {
// Session["PageDIR"] = "rtl";
// }
// else
// {
// Session["PageDIR"] = "ltr";
// }
// base.InitializeCulture();
//}
}
You had previously used Response.RedirectPermanent("~/en/Default.aspx");. A complying browser would remember this and always redirect you there.
Clear your browser cache and try again :)
As a side note, use Redirect instead of RedirectPermanent. If I'm accessing www.mysite.com from a browser and www.mysite.com RedirectPermanent's me to www.myothersite.com, a complying browser would remember this and for all future requests to www.mysite.com, it would call www.myothersite.com.

Error when Redirecting user using Global.asax file ::Page's not redirected properly

Issues that I am facing and my questions are as follows:-
I am running this code on localhost only, is there any free service or something where I can run this sample webite of mine online?
Else the IP address the follwoing code returns is always 127.0.0.1. How do I test my code? Currently I gave static values for IP address.
HTTP_X_FORWARDED_FOR is always returning null value. I have read that only HTTP_X_FORWARDED_FOR contains the real IP address of a user.
Does REMOTE_ADDR contain real IP address of a user or the IP in it can also be spoofed? I just want to know the country in which the user is sitting in so I can redirect the user based on the ISO codes.
One major issue that I am facing is that I am unable to see MasterPageFile property in this Global.asax file.
What I want is something like this :-
If the ISO code is "FR" then the master to be loaded for the site is FranceMaster.master
Else if ISO Code is "GB" then BritainMaster.master file is to be used.
In what part of this file do I set the master pages??
So basically I wanna learn how to Redirect a user based on the ISO code AND set the master page too. I gave hard coded IP address something like 122.87.234.1 then it gave me this error that Couldnt redirect properly. I googled about it and found out that the response.redirect
request was being called indefinately. If the following way is not how u redirect a user, how else one does it??
I am sure this kind of stuff has been implemented way too many times. Could someone PLEASE help me out with this one?? I would be grateful. Thanks.
below is the code I have written in Global.asax file :-
<%# Application Language="C#" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="WorldDomination.Net" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
void Application_BeginRequest(object sender, EventArgs e)
{
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
string trueIP = ipRange[le];
ip = trueIP;
}
else
{
ip = Request.ServerVariables["REMOTE_ADDR"];
}
// string userHostIpAddress = "203.1.2.3";
string userHostIpAddress = ip;
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
string country = ipAddress.Country(); // return value: UNITED STATES
string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
}
if (ipAddress.Iso3166TwoLetterCode() != null)
{
if (ipAddress.Iso3166TwoLetterCode().ToLower() == "fr")
{
Response.Redirect("www.mysite.fr");
}
if (ipAddress.Iso3166TwoLetterCode().ToLower() == "ja")
{
Response.Redirect("www.mysite.com/BlockedAccess.aspx");
}
}
else
{
Response.Redirect("www.mysite.com");
}
}
</script>
EDIT:-
#Chris :: Thanks. I am a bit confused. If the IP address can always be spoofed, how can I ever get the country of a user? I mean how to make sure the user location that I am fetching is correct? Also, there are like 100s of pages on the actual application project that I have to work on. Presently I was just trying it out by making a sample application with just one page. Now, it would be quite a task to set that masterpagefile property in ALL the pages. Is there no way to get it done in a single place?? There are literally hundereds of pages in that web application. Please reply. Thanks.
For the redirect error use Server.Transfer instead of Response.Redirect. The latter will actually initiate a new request and cause the Application_BeginRequest code to fire again, launching your user into an infinite loop.
Regarding your other questions:
Regarding hosting, I would just do a search for "free asp.net hosting", or if you're willing to pay a few bucks for better service find a web host.
HTTP_X_FORWARDED_FOR will populate when the request is forwarded via a proxy server. Since you're testing locally and never pass through a proxy server this variable will never be populated. I also noticed that your code is grabbing the last value from HTTP_X_FORWARDED_FOR instead of the first. I believe that will give you the IP address of the last proxy the user passed through and not the actual client IP (maybe someone else can confirm this).
The user's IP address can always be spoofed.
If you want to dynamically switch your masterpage you will need to do this on the Page_Init event of every page. The best solution would likely be to set a session variable with the appropriate masterpage in your global.asax and then assign that as your masterpage in the Page_Init.
Global.asax:
if (ipAddress.Iso3166TwoLetterCode().ToLower() == "fr")
{
Session["MasterPage"] = "~/FrenchMaster.master";
Server.Transfer("www.mysite.fr");
On each page's Page_Init:
this.MasterPageFile = Session["MasterPage"].ToString();
If you're going to go down this route I would suggest creating a custom page class that implements this and that all of your pages in turn inherit from so you don't actually have to copy/paste this code to each page.

Categories