Description:Sometimes you may want to get the IP Address of the client in ASP.NET. In that case you can use the
property "UserHostAddress" of the current Request object or by checking "REMOTE_ADDR" and "

HTTP_X_FORWARDED_FOR". I have
mentioned two ways to read user IP Address. You can use any of the two.

Asp.Net C# Codeto read IP Address of the client

protectedvoid Page_Load(object sender, EventArgs e)

{

Response.Write("IP Address: " +
GetIPAddress());

// Response.Write("IP
Address: " + GetIPAddress2());

}

protectedstring GetIPAddress()

{

HttpRequest request = HttpContext.Current.Request;

return request.UserHostAddress;

}

protectedstring GetIPAddress2()

{

string IPAddress = string.Empty;

IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (string.IsNullOrEmpty(IPAddress))

{

IPAddress =
Request.ServerVariables["REMOTE_ADDR"];

}

return IPAddress;

}

Asp.Net VB Codeto read IP Address of the client

ProtectedSub Page_Load(sender AsObject, e AsEventArgs) HandlesMe.Load

Response.Write("IP Address: "
& GetIPAddress())

'Response.Write("IP
Address: " & GetIPAddress2())

EndSub

ProtectedFunction GetIPAddress() AsString

Dim request AsHttpRequest = HttpContext.Current.Request

Return request.UserHostAddress

EndFunction

ProtectedFunction GetIPAddress2() AsString

Dim IPAddress AsString = String.Empty

IPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

IfString.IsNullOrEmpty(IPAddress) Then

IPAddress =
Request.ServerVariables("REMOTE_ADDR")

EndIf

Return IPAddress

EndFunction

Now over to you:

A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..
ConversionConversionEmoticonEmoticon