?
Solved

ASP.Net Comparing String IP address with Request.UserHostAddress.ToString()

Posted on 2008-10-02
6
Medium Priority
?
1,642 Views
Last Modified: 2012-05-05
I'm trying to compare the user IP address with the one in text format like "192.168.1.1".

Request.UserHostAddress.ToString () return string like "fe80::5161:9f74:13bd:960d%9". Definitely those two strings are not same.

How can I compare those two strings (IPs)? I've tried with NameValueCollection. But I dont' know how to make it work.

Could I please get some suggestions? Thank in advance...
0
Comment
Question by:winmyan
6 Comments
 
LVL 2

Expert Comment

by:Kimpzor
ID: 22632477
Try this
Request.ServerVariables["REMOTE_ADDR"].ToString();
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 22632491
try
HttpContext.Current.Request.UserHostAddress.ToString();
or
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

and also try
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

then compare which code gives you the correct format of IP address.

I IP still comes in different format than let me know.
0
 

Author Comment

by:winmyan
ID: 22635052
Thank both of you for the comments,

I've tested all.

Request.ServerVariables["REMOTE_ADDR"].ToString();
HttpContext.Current.Request.UserHostAddress.ToString();
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  // Come out nothing

They all came out "fe80::5161:9f74:13bd:960d%9" instead of "192.168.1.1". Any hope?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:prairiedog
ID: 22635084
Are you using Windows Vista?
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 2000 total points
ID: 22635327
It seems that you are using either Windows Vista or Windows Server 2008, in which IPv6 is intalled and enabled by default. So when you run the above code, IPv6 address will be returned instead of IPv4 address. Here is an article that will help you to convert an IPv6 address to an IPv4 address: http://aspnet.4guysfromrolla.com/articles/071807-1.aspx
0
 

Author Closing Comment

by:winmyan
ID: 31502571
Yes, I'm using Windows Vista. It works fine in production environment, because we all use IPV4.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question