Solved

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

Posted on 2008-10-02
6
1,573 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 22

Expert Comment

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

Accepted Solution

by:
prairiedog earned 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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