CloudfrontGet cloudfront URL instead of Orgin URL - c#

I have a domain using cloudfront in AWS.
+ test1.cloudfront.com(distribution) google.com(origin)
on browser, i type
"test1.cloudfront.com"
On my controller,i declare
var url = HttpContext.Current.Request.Url.Host;
//result: url = "google.com"; //origin url
How to get cloudfront url instead of origin url? pls help me!

Upon creation of an Amazon CloudFront distribution, a unique Domain Name will be assigned:
Use that Domain Name to access your service via CloudFront.

Related

How to get the client requested url where the request is coming in the ASP.NET web api?

In My application the scenario like i want to read the client requested URL from ASP.NET WEB API.
Example:
https://xxx.test.com/test.html page is calling https://api.test.com/api/home/get/1 WEB method.
The requested url https://xxx.test.com/test.html need to read from the web method.
The below code is returning IP Address. It is not returning domain url.
// GET api/home/get/5
public string Get(int id)
{
return HttpContext.Current.Request.UserHostAddress;
}
Please suggest me.
Thanks in Advance.
Look to HttpContext.Current.Request.UrlReferrer.OriginalString. Note that this data was set by the client, so you can't trust it's 100% accurate.
Try this :
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
If you're behind a load balancer or other reverse proxy, when asking for HttpContext.Current.Request.UserHostAddress, you won't get the IP of the client, instead you'll get the IP of the load balancer. In that case, look to HttpContext.Current.Request.Headers["X-Forwarded-For"] for the browser's IP address. Note that if you aren't behind such hardware, this is a great attack vector.
You can try HttpRequest.UrlReferrer. The MSDN reference can be found here

How to get the site name of a web application?

I made an OData Controller in a ASP.NET application and call its methods like this(e.g. from fiddler):
Local Version:
http://localhost:2343/api/SWD/GetMyEntities
<-----baseUrl-------><----static part----->
Server Version
http://myServer/myApp/api/SWD/GetMyEntities
<-----baseUrl-------><----static part----->
I know how to make the baseUrl for the local version in Silverlight. Here is the code:
string baseUrl = string.Format("{0}://{1}:{2}",
Application.Current.Host.Source.Schema,
Application.Current.Host.Source.Host,
Application.Current.Host.Source.Port);
string methodUrl = baseUrl +"/api/SWD/GetMyEntities";
But for the Server Version this code returns:
http://myServer/api/SWD/GetMyEntities
So where is the site name (myApp)? Is there any function to find out the site's name in the IIS (which is provided when publishing the application to IIS)?
If I understand correctly, I think you could use:
string hostName = System.Net.Dns.GetHostName();
or
System.Environment.GetEnvironmentVariable("COMPUTERNAME");

Getting error "invalid uri" when using http://localhost

I'm working with my first api here and I'm trying to get through the Oauth authorization so i can start working with the api. When trying to get the short life access key with the following code surveymonkey is telling me I have an invalid uri. The api key and username are replaced with dummies
string url = "https://api.surveymonkey.net/oauth/authorize";
string api_key = "api_key=sdwertyujgfv3f24qqa4kfyd";
string client_id = "client_id=XXX";
string redirect_uri="redirect_uri=http://localhost";
url = url + "?" + redirect_uri +"&" + client_id + "&" + api_key;
System.Diagnostics.Process.Start(url);
What is the uri and how do i use is in this situation.
You can not bitly a localhost link. You could try with 127.0.0.1 instead just to get it to work. But ultimately once the app is deployed it's hostname will not be localhost so it will work if that was the only obstacle.

Selenium: Find the base Url

I'm using Selenium on different machines to automate testing of a MVC Web application.
My problem is that I can't get the base url for each machine.
I can get the current url using the following code:
IWebDriver driver = new FirefoxDriver();
string currentUrl = driver.Url;
But this doesn't help when I need to navigate to a different page.
Ideally I could just use the following to navigate to different pages:
driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
driver.Navigate().GoToUrl(baseUrl+ "/Home");
A possible workaround I was using is:
string baseUrl = currentUrl.Remove(22); //remove everything from the current url but the base url
driver.Navigate().GoToUrl(baseUrl+ "/Feedback");
Is there a better way I could do this??
The best way around this would be to create a Uri instance of the URL.
This is because the Uri class in .NET already has code in place to do this exactly for you, so you should just use that. I'd go for something like (untested code):
string url = driver.Url; // get the current URL (full)
Uri currentUri = new Uri(url); // create a Uri instance of it
string baseUrl = currentUri.Authority; // just get the "base" bit of the URL
driver.Navigate().GoToUrl(baseUrl + "/Feedback");
Essentially, you are after the Authority property within the Uri class.
Note, there is a property that does a similar thing, called Host but this does not include port numbers, which your site does. It's something to bear in mind though.
Take the driver.Url, toss it into a new System.Uri, and use myUri.GetLeftPart(System.UriPartial.Authority).
If your base URL is http://localhost:12345/Login, this will return you http://localhost:12345.
Try this regular expression taken from this answer.
String baseUrl;
Pattern p = Pattern.compile("^(([a-zA-Z]+://)?[a-zA-Z0-9.-]+\\.[a-zA-Z]+(:\d+)?/");
Matcher m = p.matcher(str);
if (m.matches())
baseUrl = m.group(1);

How to generate pre-signed Amazon S3 url for a vanity domain, using amazon sdk?

I have an s3 bucket called foo.example.com, which is all CNAMEd correctly.
I'm switching to the latest AWS .net SDK.
I wish to generate pre signed url like:
http://foo.example.com/myfile.txt?s3_params_here
Note the vanity cname there.
I have:
string bucketName = "foo.example.com";
AmazonS3Client s3Client = new AmazonS3Client("bar", "xxx",
new AmazonS3Config
{
ServiceURL = bucketName,
CommunicationProtocol = Protocol.HTTP
});
string key = "myfile.txt";
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
.WithBucketName(bucketName)
.WithKey(key)
.WithExpires(DateTime.Now.AddMinutes(5))
.WithProtocol(Protocol.HTTP);
string url = s3Client.GetPreSignedURL(request);
the url I get is something like:
http://foo.example.com.foo.example.com/myfile.txt?AWSAccessKeyId=bar&Expires=1331069777&Signature=234KoUUvfE1nCcs2vLj9RQUhqF8%3D
Which is clearly wrong.
I've tried a buch of different variations with ServiceURL, bucketname, etc, but nothing seems to work.
I can't find any good documentation - what is the correct way to do this?
Thanks.
Update [workaround]
I've meanwhile resolved the contradicting test results of mine, which stem from respectively unsystematic testing and URL manipulations. The following workaround does the trick for me (i.e. tested and reproducible), simply starting from your solution:
string bucketName = "foo.example.com";
// [...]
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
.WithBucketName(bucketName)
.WithKey(key)
.WithExpires(DateTime.Now.AddMinutes(32))
.WithProtocol(Protocol.HTTP);
Now this yields the erroneous URL with a duplicate domain name, i.e. http://foo.example.com.foo.example.com/myfile.txt?[...]
The duplicate can simply be removed though, e.g.:
string url = s3Client.GetPreSignedURL(request);
// KLUDGE: remove duplicate domain name.
url = url.Replace(bucketName + "." + bucketName, bucketName);
This yields a proper working pre-signed URL for me (i.e. http://foo.example.com/myfile.txt?[...]) by working around the encountered limitation regarding the desired approach outlined below.
Justification
Manipulating the generated URL like so seems odd, but this not having an effect on the query string authentication is in line with how these signatures are created, see Query String Request Authentication Alternative, where you'll find the pseudo-grammar that illustrates the query string request authentication method:
StringToSign = HTTP-VERB + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Expires + "\n" +
CanonicalizedAmzHeaders +
CanonicalizedResource;
That is, the domain name isn't used for the signature creation at all, rather only information regarding the resource itself; section Example Query String Request Authentication right below the referenced pseudo-grammar fragment illustrates this with an actual resource.
Assessment
I don't know whether there is still a misunderstanding on our part or whether this might just be a bug in the AWS SDK for .NET, see e.g. Why is my S3 pre-signed request invalid when I set a response header override that contains a “+”? for a related bug resolved via a similar workaround as well, which has meanwhile been fixed though; accordingly, this should likely be escalated to the AWS forums and/or support channels to get an appropriate answer or solution.
Good luck!
Desired answer [dysfunctional]
The S3 CNAME handling implies the bucket name already, so all you need to do is removing your bucket name from GetPreSignedUrlRequest, i.e. it should look like so:
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
.WithKey(key)
.WithExpires(DateTime.Now.AddMinutes(5))
.WithProtocol(Protocol.HTTP);
I've tested this with a bucket of mine and it works as expected like so.
the presignedURL returns an URL object after signing the request. I have used the same and >dont have real issues, but there are some things to consider:
Ensure the object URL you are considering does not have a '//' it could easily happen if >you start the storage path starts with a "/", you would have stored the object in a path some >thing like https:///x/y/z/abc.png the key for such a resource is x/y/z/abc.png >and not /x/y/z/abc.png
If the above is ensured, then from the URL object that is returned get the query parameters >from the URL object url.getQuery() will return the query parameters which contains the >signature information, just suffix this with your original awsURL and things should work with >out any encoding issues.
Hope this helps..
Essentially you need to use url.getQuery on the returned url object rather than simply affixing it to the end of your bucket.
https://forums.aws.amazon.com/thread.jspa?threadID=70521

Categories

Resources