Solved

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

Posted on 2008-10-02
6
1,533 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now