Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a an application where users are authenticated using single sign on and enter our system requesting our webpage.
How would I get/read the http request(to my server) header values(including username and password) in to my application using c#.
I think its just like querystring instead I want to user request.header but could not find any documentation.
Any resource would be helpful.

Thanks!
Posted
Updated 16-Jul-13 4:36am
v2
Comments
lbazetto 15-Jul-13 19:09pm    
you have a code to explain this action?
Member 8544853 15-Jul-13 23:06pm    
No, I have been told to get http request header values(like name value pairs in a request) so as to get user name and password from single sign on to authorize the user.
lbazetto 16-Jul-13 17:33pm    
I do not understand your question, but see if the link can help you:

http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponseheader.aspx

1 solution

Use the following method to Get cookie...
C#
private static string GetCookie()
{
            string uri = string.Empty;
            string cookie = string.Empty;

            HttpWebResponse response = POST(uri);
            if (response == null)
            {
                this.LoginStatus = "Not connected";
                return "";
            }

            WebHeaderCollection headers = response.Headers;

            if ((response.StatusCode == HttpStatusCode.Found) ||
                    (response.StatusCode == HttpStatusCode.Redirect) ||
                    (response.StatusCode == HttpStatusCode.Moved) ||
                    (response.StatusCode == HttpStatusCode.MovedPermanently))
            {
		if (headers["Set-Cookie"] != null)
		{
		   string cookies = headers["Set-Cookie"];
                   String[] fields = Regex.Split(cookies, ";\\s*");
                   cookie = fields[0];

		}
	    }
 return cookie;
}
public static HttpWebResponse POST(string url)
{
    try
    {

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "POST";
        request.KeepAlive = true;
        request.AllowAutoRedirect = false;
        request.Accept = "*/*";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        return response;
    }
    catch
    {
       return (HttpWebResponse)null;
    }
}

Now you can use the cookie returned and use it another POST or GET, post sample below:
C#
public static HttpWebResponse POST(string postData, string url, string referer, string cookie)
{
    try
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "POST";
        request.KeepAlive = true;
        request.AllowAutoRedirect = false;
        request.Accept = "*/*";
        request.ContentType = "application/x-www-form-urlencoded";
        if (!string.IsNullOrEmpty(cookie))
            request.Headers.Add(HttpRequestHeader.Cookie, cookie);
        if (!string.IsNullOrEmpty(referer))
            request.Referer = referer;
        request.ContentLength = byteArray.Length;
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
        //   request.Proxy = null;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        try
        {
            return (HttpWebResponse)request.GetResponse();
        }
        catch
        {
            return (HttpWebResponse)null;
        }
    }
    catch
    {
        return (HttpWebResponse)null;
    }
}
 
Share this answer
 
Comments
Member 8544853 18-Jul-13 13:42pm    
Can you please let me know what this code is doing?
ersan_dolek 26-Oct-15 8:04am    
Thanks for your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900