I want to get a user IP address from asp.net web API application.
I am using angular 6 and asp.net web API. My API always returning the angular application's hosted IP address, not the user IP address. Below is the sample code which I am using
string HTTP_X_FORWARDED_FOR = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(HTTP_X_FORWARDED_FOR))
{
var s = HTTP_X_FORWARDED_FOR.Split(',');
}
string REMOTE_ADDR = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
Related
Is there a Wakanda way to:
expose a Wakanda application only to a specific IP address or IP range
validate a login request (custom login) against an IP address or IP range
I will answer the questions one by one :
1-
This should be done using your OS' firewall (using iptables for instance if you are on Linux)
2-
If you restrict access to the app using the firewall, you might not need to do this. But if you really need to :
Today there is no good way to do this, because you can't disable the authentication REST API. The workaround I propose is as follows (but I don't think it will work if you are using active directory ):
Add a custom request handler for authentication /login where you do something like :
function login(request,response){
var ip = request.remoteAddress;
if( ! isIPAuthorized(ip)){
response.statusCode = 403;
return;
}
sessionStorage["login-request"] = true;
/*
* Your login code here
* For instance you can use loginByPassword, createUserSession ..
*/
sessionStorage["login-request"] = false;
}
Inside your Login Listener you can check if the login request came from you custom login function or not by checking the sessionStorage :
if(!sessionStorage["login-request"]){
return {
"error" : 1024,
"errorMessage" : "Unautorized Login Attempt"
}
}
This way any login attempt using the default REST authentication API where IPs are not checked will be refused.
The best way to expose any application to a specific range of IP addresses (while blocking all others) is with a firewall; whether it be a software-based firewall (such as iptables or windows-firewall) or a hardware-based firewall (such as a Cisco ASA). Using a firewall completely alleviates the need for the second part of your question.
For the first question, enable the Cross-origin resource sharing in the Project Settings file ( Settings.waSettings ), and you need to define the list of Domain name or IP address from where external pages are allowed to send data requests to the Wakanda Server via CORS. Multiple domain attributes can be added to create a white list.
I have built a simple enough web application that uses the Google Books API to retrieve volume information for an ISBN the user provided. The application uses the official C# library. Requests are authorized by means of an API Key.
When running the app on my local machine I access the service with a German IP address and everything is fine. When accessing the Books API from Microsoft Azure however I get the following error:
Google.Apis.Requests.RequestError
Cannot determine user location for geographically restricted operation. [403]
Errors [
Message[Cannot determine user location for geographically restricted operation.]
Location[ - ]
Reason[unknownLocation]
Domain[global]
]
Does anyone know how to access the Google Books API from a web application hosted in Microsoft Azure?
country=us URL param solved this for me, mentioned in this github project, but not documented in the volumes list route of course
This blog says you can also use x-forwarded-for, I didn't try that
Ok I have no idea if this will work. But you can supply the ip address in your request. This may or may not work if it doesn't let me know and I will delete this.
var bookService = new BooksService(new BaseClientService.Initializer()
{
ApiKey = "xxxx",
ApplicationName = "BooksService Authentication Sample",
});
var request = bookService.Bookshelves.List("user");
request.UserIp = ""; //<-- find an ip address to feed it
var result = request.Execute();
The thing is Google normally takes the IP address the request is coming from. I wonder why it cant pick up a geo location from the IP address Azure is sending the request from.
I want to host, mutiple self-hosted webapi's along with the web-site on the same machine / windows 2k8 server. For hosting the website I am using IIS. and for webapi's I would be using self-hosted webapi 2. How do I configure using self-hosted webapi, so that everything can work in sync on same server.
So lets say I will host the website at http://example.com/mysite and I will be hosting the webapi at http://example.com/apiws and http://example.com/apiui
I am using windows service for the configuration. This is how the web-api self hosting looks like as of now - for first webapi.
protected override void OnStart(string[] args)
{
_config = new HttpSelfHostConfiguration(ServiceAddress);
_config.MapHttpAttributeRoutes();
_config.Routes.MapHttpRoute("DefaultApi",
"apiws/{controller}/{id}",
new { id = RouteParameter.Optional });
_server = new HttpSelfHostServer(_config);
_server.OpenAsync().Wait();
}
the configuration is almost same for the second server as well.
My question is having all of them working on the same port, is it possible? are there any issues which might arise? etc?
you are confusing web-api with mvc.
MVC/IIS/websites needs hosting on domain on sub-domain.
webapi are just for listening to the request and providing the data response.
I have a Windows Server 2008 R2 virtual machine with IIS 7.5 hosting a ASP.NET MVC4 website. The WinServer has a public ip address. I also purchased a domain name. The domain name hosting company forwards requests to my server.
For example, when a customer types www.mycompany.com, the request is redirected to 217.151.XXX.XXX which is my server's public ip address.
When the customer is redirected to my server, everything is working. He sees a homepage in the web browser and with address www.mycompany.com in browser's address bar. However, when he browses through different pages in the websites and got redirected, the browser's address bar displays 217.151.xxx.xxx/order/detail instead of www.mycompany.com/order/detail.
I tired IIS url rewrite which rewrites 217.151.xxx.xxx/order/detail to www.mycompany.com/order/detail
But that results in the browser freezing, waiting for a response. My guess is that when IIS rewrites the url to www.mycompany.com/order/detail, it sends a request back to the domain name hosting company. The company interprets the request and redirects it back to 217.151.xxx.xxx which causes a infinite loop.
How can I have the domain name always displayed in the borwser's bar?
A very general issue when we map domain with IP
Check this out.
http://help.lockergnome.com/windows2/Domain-static-IP-mapping--ftopict485575.html
I want to host a wcf service via windows service. And in client application I want to give feature by which client can chose which which wcf service he want to consume.
Scenario,
Host WCF in windows service on MacA
Host WCF in windows service on MacB, both services are same.
Now client will have option to choose from MacA service or from MacB service.
Thanks,
Mrinal Jaiswal
First of all you have to put the WCF url in the app.config of your client, next on the login form you can ask the user to specify the IP and Port of the service.
Here is a pice of code from one of my projects:
private void btnLogin_Click(object sender, EventArgs e)
{
string url = "net.tcp://" + txtServer.Text + ":" + txtPort.Text + "/NoxService/";
Program.Config.AppSettings.Settings["ServerAddress"].Value = url;
Program.Config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
//set server ip
Program.NoxProxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServerAddress"]);
}