What would be the fastest way to get the numerical format (NOT THE STRING FORMAT) of the client?
string format : 223.255.254.0
numeric format : 3758095872
I can postcompute this by some code like
static public uint IPAddressToLong(string ipAddress)
{
var oIP = IPAddress.Parse(ipAddress);
var byteIP = oIP.GetAddressBytes();
var ip = (uint)byteIP[0] << 24;
ip += (uint)byteIP[1] << 16;
ip += (uint)byteIP[2] << 8;
ip += byteIP[3];
return ip;
}
based on the Request.UserHostAddress string but I was hoping that IIS or ASP.NET precomputes this and it’s somewhere hidden in the HttpContext.
Am I wrong?
HttpContext does not seem to be doing any more magic than what you already see: a string value in
HttpRequest.UserHostAddressSome background info:
HttpContext.Current.Requestis of typeSystem.Web.HttpRequestwhich takes aSystem.Web.HttpWorkerRequestas parameter when instantiated.The
HttpWorkerRequestis an abstract class instantiated by hosting implementations like, in case of IIS,System.Web.Hosting.IIS7WorkerRequestwhich then implements the abstract methodGetRemoteAddress()ofHttpWorkerRequestwhich is internally used byHttpRequest.UserHostAddress.IIS7HttpWorkerRequestknows thatREMOTE_ADDRis the IIS property it needs to read and, after going through a few more layers of abstraction while passing around the request context, it all finally ends in callingMgdGetServerVariableW(IntPtr pHandler, string pszVarName, out IntPtr ppBuffer, out int pcchBufferSize);in webengine.dll which simply writes a string of lengthpcchBufferSizeintoppBuffercontaining the same stuff you get fromHttpRequest.UserHostAddress.Since i doubt that there are other parts in the HttpContext that get fed request-sender related information, i’m assuming you’ll have to keep doing your own magic for conversion for which there are plenty of ideas in the link i posted in the comments.