How to get user’s IP address using asp.net


Many times we need to get the current user’s / visitor’s Ip address.

Sometimes this is essential for security reasons.
We must leg the user’s IP address in the following situations:

1) where you accept data from user, like guestbook newsletter, search page etc. This gives a plus point to the security of website, as you can get the user’s identity by his IP if he do something wrong with your website.

2) User login pages. Alwayskeep record the logins of website users. Record the datetime and IP of user.

If you want to blok some particular users to use your website then on page load just ckeck the current Ip against database of ip’s you have created for blacklisting.
Now coming to the point,
How to get user’s IP address using asp.net

HttpContext.Current.Request.UserHostAddress;
or
HttpContext.Current.Request.ServerVariables[“REMOTE_ADDR”];

The above code may return the IP with proxy.

Use the below code to get the IP address of the machine and not the proxy use the following code
HttpContext.Current.Request.ServerVariables[“HTTP_X_FORWARDED_FOR”];


Advertisements

About dotnetcoderoom

Working as an Onsite coordinator in USA. Handling UAT, CR (Change Requests), Product Support/Development, Build Management. Supervise and assist in live product implementation (at client side) and training. Providing technical direction for the development, design, and system integration. Closely monitoring and working with offshore team for Enhancements, Bug Fixing, Enforce coding standards, perform code reviews and mentors junior developers. Proven track of timely delivery of enterprise level web based applications. Skilled in a variety of software languages, tools, and methodologies, with a special expertise in Microsoft .NET technologies. MCTS .Net 4.0 Web applications MCTS .Net 2.0 Web applications GNIIT, MCA, B.Com Specialties 8+ Years of experience in .NET Technologies. Expertise in .NET, ASP.NET, MVC, C#, VB.NET, WCF, Web Services, MS SQL, JQuery. Excellent R&D and troubleshooting skills. Extensive knowledge in E-Commerce and Healthcare domain. View all posts by dotnetcoderoom

One response to “How to get user’s IP address using asp.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: