0

Possible Duplicate:
Max length for client ip address

I need a way to block users from their IP if necessary. What's the best way to approach this? Can I use Request.ServerVariables["REMOTE_ADDR"] and store the ip address as a string in the DB? What lenght would be appropriate for the varchar?

Thanks

Community
  • 1
  • 1
Prabhu
  • 12,995
  • 33
  • 127
  • 210

2 Answers2

1

We did the same thing in one of our apps and the approach we took was to create a custom HttpHandler which was wired up to the Application_BeginRequest event.

Inside the handler we grabbed the IP address of the request by doing HttpContext.Current.Request.UserHostAddress and then performed a check against our list of blocked IP addresses. The blocked IP's are all stored in the database but we also hold a cached copy in memory in the server to avoid trips to the database on every request.

There is a pretty good article on MSDN about creating a custom HTTP Module Walkthrough: Creating and Registering a Custom HTTP Module

For what it's worth we store the IP's as strings in the DB but there is some good info on the best format to use in the posts already added.

AndyM
  • 1,148
  • 1
  • 10
  • 19
0

You could either store IP as a varchar of length 16 or you could store single integer b1 + b2 * 255 + b3 * 255^2 + b4 * 255^3 where b1 to b4 are fields from ip, as b1.b2.b3.b4 You could use other checksum but there is no point for storing long ip as string.

Sebi
  • 1,390
  • 1
  • 13
  • 22