Link to home
Create AccountLog in
Avatar of quiTech
quiTechFlag for Canada

asked on

ASP script to check if client is on same subnet as IIS server

I need to check if a client connected to an ASP site on IIS is coming from the same subnet as the the one on which the server is located.  Is this possible.  I know I can get the client and server IP address using request.servervariables.  But, without the subnet mask, it doesn't help a lot.  Of course, once I have the subnet mask, I'd still have to figure out some algorithm... hoping someone may have already done this... :-)

Thanks in advance!
Avatar of ddrudik
ddrudik
Flag of United States of America image

First, start with the local IP address:
Request.ServerVariables("LOCAL_ADDR")

Then, FernandoSoto's previous solution on this should apply:
https://www.experts-exchange.com/questions/21465331/Ip-address-within-Subnet.html

You would need to change some declarations etc. to match ASP syntax.
ASKER CERTIFIED SOLUTION
Avatar of L00M
L00M
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
L00M, unfortunately it's not as simple as that since you can have valid subnet masks that further subdivide any of the networks in Classes A/B/C.

An example would be Large ISP, Inc. that was assigned a Class B network of 165.21.0.0/255.255.0.0 to do with as they see fit.  Likely they will choose to further subnet that range so that each customer is only assigned as few addresses as necessary so that the largest number of customers are supported with the given address range.  If I'm a customer of theirs and I have a server off of their network the IP address assigned to my server would be something like 165.21.16.17/255.255.255.248 which gives me the 6 valid IP addresses in my subnet to assign to services on that server.
oops:
If intIP1 >128 and intIP1 < 192 Then strSubNet = "255.255.0.0"
should be:
If intIP1 >127 and intIP1 < 192 Then strSubNet = "255.255.0.0"
Ah, after reading ddrudik's comment, my inexperience of subnet's shine bright! Oh well, I took a stab.
I thought perhaps I could use javascript to identify the subnet mask as well, but since I run my computer on a local network, my subnet mask and IP address are different than my public IP address and subnet mask.
So, I bow out to the networking guru's.
The best bet might be to use WSH and get the subnet mask though as .vbs script, you will still need to do the calculation to determine if the other IP address is in your subnet.  To launch the .vbs as a CGI, see this technote:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;221081

But, this is the long way to get the subnet mask, since it won't likely change too often during the life of the server why not just find it manually and hardcode it for the calculation?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
If you run the WSH as a CGI and it is running server-side I'm not sure you will receive that same message.  Still, it's a long way around getting a subnet mask one can easily get manually.
has the issue been resolved?
Avatar of quiTech

ASKER

sorry for dropping the ball on this one... I actually worked around it through IIS.  

I think both your suggestions would work in the end.  Should I just split the points between the two of you?
Sounds good to me.  Thanks for the question and the points.
I was hoping I might be in on the points split, maybe it was a mis-click, but thanks for the question anyway.
Avatar of quiTech

ASKER

Yes, total screw-up on my part.  Meant to split the points 50/50 between you and L00M.  Not sure how to change it now... sorry.
It requires a message posted to community support etc., but don't worry about, no big deal.