Avatar of quiTech
quiTech
Flag 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!
ASP

Avatar of undefined
Last Comment
ddrudik

8/22/2022 - Mon
ddrudik

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
L00M

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ddrudik

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.
L00M

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"
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
L00M

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.
ddrudik

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
L00M

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ddrudik

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ddrudik

has the issue been resolved?
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?
ddrudik

Sounds good to me.  Thanks for the question and the points.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ddrudik

I was hoping I might be in on the points split, maybe it was a mis-click, but thanks for the question anyway.
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.
ddrudik

It requires a message posted to community support etc., but don't worry about, no big deal.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.