How to read values from Request in MVC app?

Viewed 48

I have Request.Params value as below

ALL_HTTP=HTTP_CONNECTION%3aKeep-Alive%0d%0aHTTP_CONTENT_LENGTH%3a0%0d%0aHTTP_ACCEPT%3atext%2fhtml%2c+application%2fxhtml%2bxml%2c+image%2fjxr%2c+%2f%0d%0aHTTP_ACCEPT_ENCODING%3agzip%2c+deflate%0d%0aHTTP_ACCEPT_LANGUAGE%3aen-US%0d%0aHTTP_HOST%3alocalhost%3a81%0d%0aHTTP_REFERER%3ahttps%3a%2f%2flogin.live.com%2fppsecure%2fpost.srf%3fresponse_type%3dcode%26client_id%26redirect_uri%3dhttps%253a%252f%252flogin.microsoftonline.com%252fcommon%252ffederation%252foauth2%26state%26username%3dtestuser%2540hotmail.com%26max_age%3d0%2fROOT&APPL_PHYSICAL_PATH=D%3a%5cASPNETMembershipProfile%5cDeployments%5c&AUTH_TYPE=&AUTH_USER=&AUTH_PASSWORD=&LOGON_USER=&REMOTE_USER=&CERT_COOKIE=&CERT_FLAGS=&CERT_ISSUER=&CERT_KEYSIZE=&CERT_SECRETKEYSIZE=&CERT_SERIALNUMBER=&CERT_SERVER_ISSUER=&CERT_SERVER_SUBJECT=&CERT_SUBJECT=&CONTENT_LENGTH=0&CONTENT_TYPE=&GATEWAY_INTERFACE=CGI%2f1.1&HTTPS=off&HTTPS_KEYSIZE=&HTTPS_SECRETKEYSIZE=&HTTPS_SERVER_ISSUER=&HTTPS_SERVER_SUBJECT=&INSTANCE_ID=2&INSTANCE_META_PATH=%2fLM%2fW3SVC%2f2&LOCAL_ADDR=%3a%3a1&PATH_INFO=%2fLogin.aspx&HTTP_DNT=1&HTTP_FRONT_END_HTTPS=on&HTTP_X_FORWARDED_FOR=20.689.292.800

I want to read username value from above string.

I tried below

Request.QueryString["username"].ToString();
Request["username"];
Request.Params.Get("username");

But all are returning empty or null values.

Kindly let me know how to read user name from above Request.

2 Answers

You can use:

var username = Request.Parameters["username"];

Try this

var requestParams = Request.Params.Get("ALL_HTTP");
var decoded = System.Web.HttpUtility.UrlDecode(requestParams);
var queryString = System.Web.HttpUtility.ParseQueryString(decoded);
var userName = queryString["username"];

Request.Params.Get("username") won't work because username(encoded) is inside ALL_HTTPparameter.

You must follow these steps to get username

  1. Get ALL_HTTP parameter
  2. URLDecode the value
  3. Parse in into Collections.Specialized.NameValueCollection
  4. Access the desired value using the parameter name
Related