How to find the request sender Url in my controller action? - c#

I'm using Asp.Net MVC. I want to know the url from which a request has been sent to my controller action.For example my controller action is(Customer/Index) and I want to get the url from which, my action is called.(I don't need the current URL).I want the source URL from which the request sent to me. Is there any solution?

You could try using url referrer e.g.
Request.UrlReferrer
Request.QueryString["Referrer"].ToString()

Have a look at this link: MDN reference
It states that "A Referer header is not sent by browsers if: The referring resource is a local "file" or "data" URI."
If you want to have that available if your page calls 'itself' i.e. another page/controller in you web application, you can try to add the header manually on a per request basis.

Related

Get final redirect URL

I am writing a code to get through authentication to a API based web site. I have the API key that the site needs during the login process. When I call the login method with the API key, it is supposed to redirect to a predefined URL whose parameter will then contain the request token.
e.g on firing the URL in the browser,
https://kite.trade/connect/login?api_key=hcwmefsivttbchla
I am redirected to
https://impacted-purposes.000webhostapp.com/?status=success&request_token=nb0vrfota9ott1r02q153pk3422joruf
(The request token will change in every run)
Notice the request token in the URL on the redirected URL. That's what I need to get from the code.
So, I use a the code that is referred here GetFinalRedirect:
Getting the Redirected URL from the Original URL
ie. I call:
GetFinalRedirect("https://kite.trade/connect/login?api_key=hcwmefsivttbchla")
However, I don't get the final redirect. I understand there could be a Javascript redirect, but checking the response, doesn't suggest so.
Any help pls to get the final URL so that I can parse the request token from it.
Well, I can't test this since i don't have an account. And I hope thats not your real-api-key...
But the function you're using is just sending a HEAD request to server. A HEAD request has no response. With the HEAD request you will only get redirects that are included in the HTTP headers. If the redirect is done with HTML-META tags or with javascript you have to send a GET (or POST) request...
If that fails too set a breakpoint inside the function and look at the received http-headers and the response text...

MVC Redirect with headers

In an action method I want to redirect to a third party url. They collect some info from custom headers.
I have understood that I can not redirect the user, for example:
return RedirectResult(some url);
since the browser will not reattach my custom headers..
How do I do this? Is this a wrong approach?
They collect some info from custom headers.
So you can't redirect. You need to read their documentation. You're most likely expected to do an HTTP request to their server and show the user the response.

Redirect and pass form variables

I am integrating with a 3rd party payment platform (Stripe). Their API is posting back a bunch of parameters in the HTTP post to an aspx page. What I need to do is read one of the FORM variables to know where the post should be forwarded to (i.e. it needs to be redirected to the user specific subdomain endpoint - https://user1.mayapp.com/newstripeaccount.aspx).
My questions is how in .NET can you forward the request and preserve all the http post parameters without meticulously parsing them out and using HttpWebRequest to construct a new HTTP post to the final endpoint. Is there a simpler way to redirect and just pass the httpcontext?

Check if redirected to default page

How to check using C# if "redirect" to "default document" happened?
For example, in browser I type URL: mysite.com/. When on server I check HttpContext.Current.Request.Url.AbsoluteUri, i receive mysite.com/default.aspx...
How I can get the exact URL that user has in his browser?
Thanks
EDIT: After some questions about the needs, I will give more details.
I have page with default.aspx with iframe inside of it. The iframe src is not the same origin (default.aspx is http and iframe content is https). On server side, i need to set the query string param to the src of iframe to include the exact URL that user has in browser. I need it in order to be able to set parent.location = parentURL + '#myparam' on iframe client side.
Currently everithing works fine, except when the request made to domain name without providing file name.
Try HttpContext.Current.Request.RawUrl
You typed
mysite.com/.
and you get
mysite.com/default.aspx...
Because you have set default.aspx as the default / Start up page in your site. The browser always redirect to the default page. I think when we type mysite.com the asp.net automatically appends the default page in the URL, so when we use Request.Url we get the mysite.com/default.aspx
Reading your intention of the IFrame, perhaps you are looking for Framset Script to determine the redirection ?
if (parent.location.href==window.location.href)
{
// you re-direction codes...
}
EDIT :
Giving a different HTTP and HTTPS, it's likely the Same Origin Policy kicked in. There is a workaround you could use PostMessage interface for cross sites.
Other option would be managed by Server(IIS) so that both http/https url request setting to default document , so that you don't need to alter client-side scripting for such complication handling.
You should delete 'Default.aspx' page from your IIS Default document list. then you get exact URL that user entered.

Request.Url and ReferrerUrl in HttpModule

We are finding HttpContext.Current.Request.Url.ToString() and HttpContext.Current.Request.UrlReferrer.ToString() values in an httpmodule. Is there any difference in getting the values from the normal page load?
Suppose if the request is coming from a google search result, then what will be the output of those two values.
HttpContext.Current.Request.Url points to the resource on your server while UrlReferrer is the URL which requested the resource.
if the request is coming from google search, UrlReferrer will give you google url something like http://www.google.com/search?q=[some text]
From MSDN (http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx):
Url: Gets information about the URL of the current request.
UrlReferrer: Gets information about the URL of the client's previous request that linked to the current URL.

Categories

Resources