[REPOST] – ASP.NET Server Variables
I have admitted in the past that I am not an experienced ASP.NET developer, so this may be a “DUH” post, but I thought it was a good idea and wanted to share.
For the ASP.NET MVC application I’ve been working on, I needed to send the user to another domain (on the same server) for some processing, after which they are returned to the original domain. I wanted to be able to verify that the return trip was actually coming from the correct domain and port, so I needed to work with some of the ServerVariables. I hunted around a bit and found the HttpRequest.ServerVariables collection that contains the data I needed as well as a wealth of other information. Working with it though, I felt a little out of date needing to supply the string names of the variables (they are Key values in a System.Collections.Specialized.NameValueCollection object), so I created a simple class so that would let me take advantage of Intellisense and let me work with the values by name.
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;namespace YourNamespace
{
public class ServerVariables
{
public string ALL_HTTP { get; private set; }
public string ALL_RAW { get; private set; }
public string APPL_MD_PATH { get; private set; }
public string APPL_PHYSICAL_PATH { get; private set; }
public string AUTH_TYPE { get; private set; }
public string AUTH_USER { get; private set; }
public string AUTH_PASSWORD { get; private set; }
public string LOGON_USER { get; private set; }
public string REMOTE_USER { get; private set; }
public string CERT_COOKIE { get; private set; }
public string CERT_FLAGS { get; private set; }
public string CERT_ISSUER { get; private set; }
public string CERT_KEYSIZE { get; private set; }
public string CERT_SECRETKEYSIZE { get; private set; }
public string CERT_SERIALNUMBER { get; private set; }
public string CERT_SERVER_ISSUER { get; private set; }
public string CERT_SERVER_SUBJECT { get; private set; }
public string CERT_SUBJECT { get; private set; }
public string CONTENT_LENGTH { get; private set; }
public string CONTENT_TYPE { get; private set; }
public string GATEWAY_INTERFACE { get; private set; }
public string HTTPS { get; private set; }
public string HTTPS_KEYSIZE { get; private set; }
public string HTTPS_SECRETKEYSIZE { get; private set; }
public string HTTPS_SERVER_ISSUER { get; private set; }
public string HTTPS_SERVER_SUBJECT { get; private set; }
public string INSTANCE_ID { get; private set; }
public string INSTANCE_META_PATH { get; private set; }
public string LOCAL_ADDR { get; private set; }
public string PATH_INFO { get; private set; }
public string PATH_TRANSLATED { get; private set; }
public string QUERY_STRING { get; private set; }
public string REMOTE_ADDR { get; private set; }
public string REMOTE_HOST { get; private set; }
public string REMOTE_PORT { get; private set; }
public string REQUEST_METHOD { get; private set; }
public string SCRIPT_NAME { get; private set; }
public string SERVER_NAME { get; private set; }
public string SERVER_PORT { get; private set; }
public string SERVER_PORT_SECURE { get; private set; }
public string SERVER_PROTOCOL { get; private set; }
public string SERVER_SOFTWARE { get; private set; }
public string URL { get; private set; }
public string HTTP_CONNECTION { get; private set; }
public string HTTP_KEEP_ALIVE { get; private set; }
public string HTTP_ACCEPT { get; private set; }
public string HTTP_ACCEPT_CHARSET { get; private set; }
public string HTTP_ACCEPT_ENCODING { get; private set; }
public string HTTP_ACCEPT_LANGUAGE { get; private set; }
public string HTTP_COOKIE { get; private set; }
public string HTTP_HOST { get; private set; }
public string HTTP_REFERER { get; private set; }
public string HTTP_USER_AGENT { get; private set; }
private ServerVariables()
{
}
public static ServerVariables Factory(NameValueCollection vars)
{
ServerVariables sv;
if (vars == null)
{
sv = new ServerVariables();
}
else
{
sv = new ServerVariables()
{
ALL_HTTP = vars[“ALL_HTTP”],
ALL_RAW = vars[“ALL_RAW”],
APPL_MD_PATH = vars[“APPL_MD_PATH”],
APPL_PHYSICAL_PATH = vars[“APPL_PHYSICAL_PATH”],
AUTH_TYPE = vars[“AUTH_TYPE”],
AUTH_USER = vars[“AUTH_USER”],
AUTH_PASSWORD = vars[“AUTH_PASSWORD”],
LOGON_USER = vars[“LOGON_USER”],
REMOTE_USER = vars[“REMOTE_USER”],
CERT_COOKIE = vars[“CERT_COOKIE”],
CERT_FLAGS = vars[“CERT_FLAGS”],
CERT_ISSUER = vars[“CERT_ISSUER”],
CERT_KEYSIZE = vars[“CERT_KEYSIZE”],
CERT_SECRETKEYSIZE = vars[“CERT_SECRETKEYSIZE”],
CERT_SERIALNUMBER = vars[“CERT_SERIALNUMBER”],
CERT_SERVER_ISSUER = vars[“CERT_SERVER_ISSUER”],
CERT_SERVER_SUBJECT = vars[“CERT_SERVER_SUBJECT”],
CERT_SUBJECT = vars[“CERT_SUBJECT”],
CONTENT_LENGTH = vars[“CONTENT_LENGTH”],
CONTENT_TYPE = vars[“CONTENT_TYPE”],
GATEWAY_INTERFACE = vars[“GATEWAY_INTERFACE”],
HTTPS = vars[“HTTPS”],
HTTPS_KEYSIZE = vars[“HTTPS_KEYSIZE”],
HTTPS_SECRETKEYSIZE = vars[“HTTPS_SECRETKEYSIZE”],
HTTPS_SERVER_ISSUER = vars[“HTTPS_SERVER_ISSUER”],
HTTPS_SERVER_SUBJECT= vars[“HTTPS_SERVER_SUBJECT”],
INSTANCE_ID = vars[“INSTANCE_ID”],
INSTANCE_META_PATH = vars[“INSTANCE_META_PATH”],
LOCAL_ADDR = vars[“LOCAL_ADDR”],
PATH_INFO = vars[“PATH_INFO”],
PATH_TRANSLATED = vars[“PATH_TRANSLATED”],
QUERY_STRING = vars[“QUERY_STRING”],
REMOTE_ADDR = vars[“REMOTE_ADDR”],
REMOTE_HOST = vars[“REMOTE_HOST”],
REMOTE_PORT = vars[“REMOTE_PORT”],
REQUEST_METHOD = vars[“REQUEST_METHOD”],
SCRIPT_NAME = vars[“SCRIPT_NAME”],
SERVER_NAME = vars[“SERVER_NAME”],
SERVER_PORT = vars[“SERVER_PORT”],
SERVER_PORT_SECURE = vars[“SERVER_PORT_SECURE”],
SERVER_PROTOCOL = vars[“SERVER_PROTOCOL”],
SERVER_SOFTWARE = vars[“SERVER_SOFTWARE”],
URL = vars[“URL”],
HTTP_CONNECTION = vars[“HTTP_CONNECTION”],
HTTP_KEEP_ALIVE = vars[“HTTP_KEEP_ALIVE”],
HTTP_ACCEPT = vars[“HTTP_ACCEPT”],
HTTP_ACCEPT_CHARSET = vars[“HTTP_ACCEPT_CHARSET”],
HTTP_ACCEPT_ENCODING= vars[“HTTP_ACCEPT_ENCODING”],
HTTP_ACCEPT_LANGUAGE= vars[“HTTP_ACCEPT_LANGUAGE”],
HTTP_COOKIE = vars[“HTTP_COOKIE”],
HTTP_HOST = vars[“HTTP_HOST”],
HTTP_REFERER = vars[“HTTP_REFERER”],
HTTP_USER_AGENT = vars[“HTTP_USER_AGENT”]
};
}
return sv;
}
}
}
Again, I realize this is not some brilliant idea, but I thought I’d share for anyone else who might want it. To make this work, be sure to include a reference to System.Collections.Specialized.