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");
Related
My Setup
Development machine:
Windows 10
IIS express
Staging machine:
Ubuntu 18.04
Nginx
My problem
I have a simple, yet strange problem.
In the block of code below, I generate an email confirmation link. Now, I want to replace the domain of the generated URL with another one.
// class fields
private readonly string _WebServerRelativeUrl = "https://www.specific-domain.com/account/";
private readonly string _BingoServerRelativeUrl = "https://www.specific-domain.eu/api/v1/identity/";
private readonly string _BingoLocalServerRelativeUrl = "https://localhost:44375/api/v1/identity/";
// code excerpt from confirm email method
var passwordResetLink = _urlHelper.Action("ResetPassword", "identity",
new { email = appUser.Email, token, lang }, _httpRequest.HttpContext.Request.Scheme);
var url = Regex.Replace(passwordResetLink, _BingoServerRelativeUrl, _WebServerRelativeUrl, RegexOptions.IgnoreCase);
if (_environment.IsDevelopment())
{
url = Regex.Replace(passwordResetLink, _BingoLocalServerRelativeUrl, _WebServerRelativeUrl, RegexOptions.IgnoreCase);
}
On localhost, all works fine. The server generates a URL as follows:
https://localhost:44375/api/v1/identity/confirmemail?userId=abcd&token=abcd
And then the https://localhost:44375/api/v1/identity is successfully replaced with https://www.specific-domain.com/account/
However, in a production environment, the application is running on Ubuntu 18.04, behind Nginx with a specific domain. Now, this string replacement doesn't work when the application is in the production environment on the Linux VM.
I also tried the String.Replace() instead of the regex, the same result. What could be the issue?
Debugging this problem would require quite a bit of time. Found an easier solution by building the initially desired URL with the Flurl library. The solution requires less code.
var url = WebServerRelativeUrl
.AppendPathSegment(ResetPassPath)
.SetQueryParams(new
{
email = appUser.Email, token = token, lang = lang
});
P.S I prefer this one over UriBuilder because it offers Fluent interface.
Please I am trying to get my domain url using the Request.Url.AbsoluteUri in asp.net core 2.0 but "Request" is not in the System.Web namespace. and this is the popular way of achieving this. Please is it different in ASP.NET core 2.0. Please help.
Below is the line of code
var verifyUrl = "/user/VerifyAccount/" + activationCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl);
Normally "link" should give me "http://myDomainName/user/VerifyAccount/276ye6tfhsgt63t6e"
but the System.Web.Request is not working
The HttpRequest object is different in ASP.NET Core. It no longer contains Url property. You'll need to build your URI manually:
var uriBuilder = new UriBuilder
{
Scheme = Request.Scheme,
Host = Request.Host.ToString(),
Path = $"/user/VerifyAccount/{activationCode}"
};
var link = uriBuilder.Uri.AbsoluteUri;
The Request variable will be available by default in your controller and such. If you find that there is no Request variable, you can try HttpContext.Request. If you're doing this inside a view, you'll need ViewContext.HttpContext.Request.
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
I have a Controller Action in which I:
Forward a URL to a 3rd party that the 3rd party re-directs the client to if an operation is successful.
The client then connects to the 3rd party and performs an operation.
If successful the 3rd party re-directs (using a 302) the client to the URL we told them at step 1.
This all works. Problem is the URL the Controller on the server tells the 3rd party about us built using:
var urlHelper = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
var responseUrl = urlHelper.Action("Action", "Controller", new { id }, Request.Url.Scheme);
responseURL now looks like:
"https://some.internal.domain/App/Action/1234".
The client however accessed the site using an address like:
"https://example.com/App/Action/1234".
and they cannot access https://some.internal.domain externally.
So how can I build the absolute URL for the 3rd party to re-direct the client to using the URL the client is accessing the site from?
UPDATE: The above method should have worked it turns out the problem is am behind a Reverse Proxy (have re-worded the title to reflect this).
I came across a similar problem. You basically have two options.
Use the URL Rewrite Module to rewrite your entire response by replacing/rewriting all pattern matched with https://some.internal.domain to https://example.com/App. You'll probably want to add certain conditions like only rewrite if the response type is of type html/text and what not.
Use an extension method. This method requires the Reverse Proxy in question to forward additional headers identifying where the original request came from (e.g. Azure Application Gateway sends a header named X-Original-Host but I think most other reverse proxies use X-Forwarded-For or some variant like Winfrasoft X-Forwarded-For). So based on the example provided you could do something like.
The helper could look like this
public static class UrlHelpers
{
public static string GetUrlHostname(this UrlHelper url)
{
var hostname = url.RequestContext.HttpContext.Request.Headers["X-Original-Host"] ?? url.RequestContext.HttpContext.Request.Url.Host;
return hostname;
}
}
And then to use it based on your example.
var urlHelper = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
var responseUrl = urlHelper.Action("Action", "Controller", new { id }, Request.Url.Scheme, urlHelper.GetUrlHostname());
I'm following a tutorial on this link http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
Now I'm stuck at these codes
private const string DummyPageUrl =
"http://localhost/TestCacheTimeout/WebForm1.aspx";
private void HitPage()
{
WebClient client = new WebClient();
client.DownloadData(DummyPageUrl);
}
My local application address has a port number after "localhost", so how can I get the full path (can it be done in Application_Start method)? I want it to be very generic so that it can work in any cases.
Thanks a lot!
UPDATE
I tried this in the Application_Start and it runs fine, but return error right away when published to IIS7
String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
If it is calling back to the same server, perhaps use the Request object:
var url = new Uri(Request.Url, "/TestCacheTimeout/WebForm1.aspx").AbsoluteUri;
Otherwise, store the other server's details in a config file or the database, and just give it the right value.
But a better question would be: why would you talk via http to yourself? Why not just call a class method? Personally I'd be using an external scheduled job to do this.
You need an answer that works when you roll out to a different environment that maybe has a virtual application folder.
// r is Request.Url
var url = new Uri(r, System.Web.VirtualPathUtility.ToAbsolute("~/Folder/folder/page.aspx")).AbsoluteUri;
This will work in all cases and no nasty surprises when you deploy.
I suspect you're using the ASP.NET development server that's built-in to Visual Studio, which has a tendency to change port numbers by default. If that's the case, then you might try simply configuring the development server to always use the same port, as described here. Then just add the port number to your URL, like so:
private const string DummyPageUrl =
"http://localhost:42001/TestCacheTimeout/WebForm1.aspx";