Please see the below codes :
private string GetUserIPAddress()
{
string User_IPAddress = string.Empty;
string User_IPAddressRange = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(User_IPAddressRange))//without Proxy detection
{
User_IPAddress = Request.ServerVariables["REMOTE_ADDR"];
//or
//Client_IPAddress = Request.UserHostAddress;
//or
//User_IPAddress = Request.ServerVariables["REMOTE_HOST"];
}
else////with Proxy detection
{
string[] splitter = { "," };
string[] IP_Array = User_IPAddressRange.Split(splitter,
System.StringSplitOptions.None);
int LatestItem = IP_Array.Length - 1;
User_IPAddress = IP_Array[LatestItem - 1];
//User_IPAddress = IP_Array[0];
}
return User_IPAddress;
}
In the case of :
1-
User_IPAddress = Request.ServerVariables["REMOTE_ADDR"];
and
Client_IPAddress = Request.UserHostAddress;
and
User_IPAddress = Request.ServerVariables["REMOTE_HOST"];
is the lower or middle line an alternate for the other lines?
Would you please give us some explain about these lines?
What are the differences ?
2-
User_IPAddress = IP_Array[LatestItem - 1];
and
User_IPAddress = IP_Array[0];
Which line should I use?
Would you please give explain about these lines?
I Don't know the class but chances are Request.UserHostAddress is an alias for Request.ServerVariables["REMOTE_ADDR"]. Also REMOTE_HOST would be the hostname but in most cases will just be the ipaddress.
Format of X-Forwarded-For is client1, proxy1, proxy2. So you want the second one. User_IPAddress = IP_Array[0];
Just remember "Since it is easy to forge an X-Forwarded-For field the given information should be used with care."
Related
I try to get image url using regex, and my code is below.
string IMG_REX_PATTERN = #"<[Ii][Mm][Gg][^>]*src\s*=\s*[\""\']?(?<IMAGE_URL>[^""'>\s]*)[\""\']?[^>]*>";
Match match = Regex.Match(result[i]["N_Dext5EditorField"].ToString(), IMG_REX_PATTERN, RegexOptions.IgnoreCase);
string src = string.Empty;
if (match.Length > 0)
{
//portalUrl = https://test.beta.co.kr
string portalUrl = DevelopmentHelper.GetPortalUrl();
src = match.Groups[1].Value.Replace(portalUrl, "");
}
else
{
src = "";
}
But, sometimes src contains port number like this.
src = :443/dext5editordata/2017/12/20171228_191217524_37634.png
I want to get url like this(not included port number)
src = /dext5editordata/2017/12/20171228_191217524_37634.png
I thought easy way is portalUrl + ":443", but port number is sometimes diffrent.
How can I fix my code? Please help me.
Just update it
string src = ":443/dext5editordata/2017/12/20171228_191217524_37634.png";
int firstindex=src.IndexOf("/");
string witoutportno=src.Substring(firstindex);
You can also Check my code here
http://rextester.com/CRS51521
based on your replies, I changed my code.
if (match.Length > 0)
{
//portalUrl = https://test.beta.co.kr
string portalUrl = DevelopmentHelper.GetPortalUrl();
string tempsrc = match.Groups[1].Value;
var uri = new UriBuilder(tempsrc);
string targetUrl = uri.Uri.ToString();
src = targetUrl .Replace(portalUrl, "");
}
else
{
src = "";
}
I'm trying to add a firewall rule for TCP Port 1433, with a specific group using the NetFwTypeLib library.
But adding the port into the LocalPorts variable as integer converted into a string or just as a simple "1433" string, returns a Value out of range exception.
Removing the port and just using all ports works fine.
Here is the code I used:
bool CreateRule(string sName, string sPort, NET_FW_IP_PROTOCOL_ ProtocolType, string sIpAdress, string sGroup = "")
{
try
{
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
firewallRule.Description = "Used to allow Server access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.Enabled = true;
firewallRule.Name = sName;
firewallRule.Grouping = sGroup;
firewallRule.LocalPorts = sPort; // "1433" causes out of range exception
firewallRule.RemoteAddresses = sIpAdress;
firewallRule.Protocol = (int)ProtocolType;
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
return true;
}
catch
{
return false;
}
}
Setting the firewallRule.LocalPorts member causes the exception.
Does someone have an idea what's going wrong?
You have to put the Protocol type before the Port, so it is valid.
I have a method named getIP() which returns the clients ip as a string.
How do I use this IP to get the location of the client using this service.
This is how i show the clients IP address.
string IP = getIP();
lblIPAddress.Text = "IP " + IP;
How do i include the clients location in ?
i.e. lblIPAddress.Text = "IP " + IP+ "location" ;)
Below you can find a very simple snippet which I was using to get data from XML endpoint of that API some time ago (I believe there was no changes to the API so it still should work):
string city;
string country;
string countryCode;
decimal longitude;
decimal latitude;
XmlTextReader hostIPInfoReader = new XmlTextReader("http://api.hostip.info/?ip=" + IP);
while (hostIPInfoReader.Read()) {
if (hostIPInfoReader.IsStartElement()) {
if (hostIPInfoReader.Name == "gml:name")
city = hostIPInfoReader.ReadString();
if (hostIPInfoReader.Name == "countryName")
country = hostIPInfoReader.ReadString();
if (hostIPInfoReader.Name == "countryAbbrev")
countryCode = hostIPInfoReader.ReadString();
if (hostIPInfoReader.Name == "gml:coordinates") {
string[] coordinates = hostIPInfoReader.ReadString().Split(new char[] { ',' });
longitude = decimal.Parse(coordinates[0]);
latitude = decimal.Parse(coordinates[1]);
}
}
}
This code can be of course improved but I believe it`s a good starting point for you.
How to get the user machine account from which he access the application in the case of Form authentication .
I use the following method but it doesn't get the required data:
protected string[] TrackUser()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string IP = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
string compName = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName);
string account = Request.ServerVariables["AUTH_USER"];
string[] user_network_data = new string[3];
if (!string.IsNullOrEmpty(IP))
{
string[] addresses = IP.Split(',');
if (addresses.Length != 0)
{
IP = addresses[0];
}
}
else
{
IP = context.Request.ServerVariables["REMOTE_ADDR"];
}
user_network_data[0] = IP;
user_network_data[1] = compName;
user_network_data[2] = account;
return user_network_data;
}
I think you're just grabbing the wrong initial information from the request. Try these:
string IP = context.Request.UserHostAddress;
string compName = context.Request.UserHostName;
string account = context.Request.LogonUserIdentity.Name;
I think the rest of your code should work fine once you have the right data to start off with.
I have been trying to figure this out but cannot find a reliable way to get a clients IP address when making a request to a page in asp.net that works with all servers.
One method is to use Request object:
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text = Request.UserHostAddress;
}
IpAddress=HttpContext.Current.Request.UserHostAddress;
Request.ServerVariables["REMOTE_ADDR"]
To access an index or property on C#, you should use [ ] instead of ( )
Use this code:
public static string GetIpAddress()
{
return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
}
System.Web.HttpContext.Current.Request.UserHostAddress;
Try this code:
var IpAddress = Request.Headers["Referer"][0];
If there are proxies between client and server. HTTP_X_FORWARDED_FOR header can be used.
var ips = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
var clientIp = "";
if (!string.IsNullOrEmpty(ips))
{
string[] addresses = ips.Split(',');
if (addresses.Length != 0)
{
clientIp = addresses[0];
}
}
else
{
clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
You can use HttpContext with property bellow:
var _request1 = HttpContext.Current.Request.UserHostAddress;
string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
string requestScheme = HttpContext.Current.Request.Url.Scheme;
string requestQueryString = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
string requestUrl = HttpContext.Current.Request.ServerVariables["URL"];