Solved

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

Posted on 2008-10-02
6
1,545 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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