Hello guys I am trying to retrieve my ip address using the following c# code and all I got is the 127.0.0.0. IP address. I need to display my IPA like which would display on google searh when I type what is my ip address.
Can you help? Thank you so much
HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
Request.ServerVariables("REMOTE_HOST")
Request.UserHostAddress()
Request.UserHostName()
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
I've tried this as well but it throw an exception "only ipv4 is supported"
You can also try the below:
using System;
using System.Web;
namespace WebApplication1
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Get request.
HttpRequest request = base.Request;
// Get UserHostAddress property.
string address = request.UserHostAddress;
// Write to response.
base.Response.Write(address);
// Done.
base.CompleteRequest();
}
}
}
Related
Hi is there any way to create my own page in my web server so that i can ping it from external application to get the WAN ip from where the application is hosted? Something like whatismyip but on my own server.
What about next code:
using System;
using System.Web;
namespace WebApplication1
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender,
EventArgs e)
{
// Get request.
HttpRequest request = base.Request;
// Get UserHostAddress property.
string address = request.UserHostAddress;
// Write to response.
base.Response.Write(address);
// Done.
base.CompleteRequest();
}
}
}
I want to make my ASP.NET MVC 3 web app to run on particular hostname or IP address only. If someone tries to host the site on different host or IP address, the website should stop working as it see the hostname/IP address different than configured (basically, hardcoded in the app DLL).
Any idea how effectively this can be achieved in ASP.NET MVC?
In your Global.asax file create new BeginRequest function:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (application.Request.Url.Host != "mydomain.com")
{
application.CompleteRequest();
}
}
Filter requests using HTTP Module.
Another way to accomplish it
is creating an action attribute to validate that request :P
public class HostValidatorAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
//validate here and returns true if valid
}
}
I'm using the following HttpModule to stop a couple of IPs that constantly attempt to spam my contact form. I don't get the spam as they trigger System.Web.HttpRequestValidationException but I do get the Exception report in my email inbox. It's not quite as annoying but almost.
I eventually want to test against either a list of IPs from the database or maybe implement the HttpBL api to test against known blacklisted IPs but doing this on every request seems to be overkill. Either way I do it, whether using IPs in the database or making requests to an external blacklist at every page request surely seems unnecessary. Can you point me in the direction of checking this once and if the IP passes the test the first time to stop checking?
using System;
using System.Web;
namespace DomainModel.Services
{
public class BlockIPModule : IHttpModule
{
public void Dispose() {}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string currentIP = context.Request.UserHostAddress;
if (!IsIpValid(currentIP))
{
context.Response.StatusCode = 403;
}
}
private bool IsIpValid(string checkIP)
{
return (checkIP != "213.5.70.205" && checkIP != "188.92.75.82");
}
}
}
updated code removed - terrible idea.
HTTP is stateless, and therefore, you really do have to check each request. You could cache the blacklist of IP addresses so you don't have to load it each time, but you'll still need to always run some test.
If you have control over it, the other option would be to do some filtering on your router. That would free your code from having to do it.
We send out registration urls to clients via email. Some of the email clients are turning the url into
url <url>
I think it may be happening when users forward the email onto themselves at which point the email client re-formats the original email (maybe)
E.g.
https://my.app.com/login.aspx?param=var
Becomes
https://my.app.com/login.aspx?param=var%20%3Chttps://my.app.com/login.aspx?param=var%3E
Which rightly produces System.Web.HttpRequestValidationException: A potentially dangerous Request.QueryString value was detected
Where in the code should I intercept these instances and santize the url so that the user is re-directed onto the original form of the url?
global.asax?
Page_Init?
HttpHandler?
Pipeline?
You can catch it in Global Application_BeginRequest or in the same event in an HttpModule.
Global
using System;
using System.Web;
namespace MassageIncomingRequestUrl
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication) sender;
string path = app.Context.Request.Url.PathAndQuery;
int pos = path.IndexOf("%20%3C");
if (pos > -1)
{
path = path.Substring(0, pos);
app.Context.RewritePath(path);
}
}
}
}
Module
using System;
using System.Web;
namespace MassageIncomingRequestUrl
{
public class UrlMungeModule : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
}
public void Dispose()
{
//nop
}
#endregion
private static void BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
string path = app.Context.Request.Url.PathAndQuery;
int pos = path.IndexOf("%20%3C");
if (pos>-1)
{
path = path.Substring(0,pos);
app.Context.RewritePath(path);
}
}
}
}
This will get your request processed with the correct query string in the Request, regardless of what you see in the browser address. You may be able to take extra steps to remove the garbage from the reported url but that is mainly just aesthetics.
i have a web page written in vb, i need to start a windows service that will be installed in the server.
You first need to add a reference to the System.ServiceProcess assembly. The following code gives you roughly what you want to do (I am using a Label control called messageLabel in the following):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class StartService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string serviceName = "Remote Registry";
try
{
StartServiceByName(serviceName);
}
catch (Exception ex)
{
messageLabel.Text = ex.ToString().Replace("\r\n", "<BR>");
return;
}
messageLabel.Text = String.Format("Service {0} started.", serviceName);
}
private void StartServiceByName(string serviceName)
{
ServiceController serviceController = new ServiceController(serviceName);
serviceController.Start();
}
}
However, there is a further thing - you need to have the web server have permission to change this service - which is something that normally can be only done with Administration rights.
haven't tested.
pleas try if works or not. you could add following code in btn click event.
dim controller as new ServiceController
controller.MachineName = "." //try the machine name
controller.ServiceName = "service name"
dim status as string = controller.Status.ToString
' Stop the service
controller.Stop()
' Start the service
controller.Start()
Depending on the permissions that the web site account has, you can start / stop services.
In addition to what others have answered, you can shell out NET START with the appropriate parameters.
You can also do this for remote computers as long as the permissions are granted (I think it has to be a domain account for this).