0

I have a .net website on IIS that has an virtual directory pointing to MVC application. I am trying to reuse a sitemaster.master on the RAzor view header.

I have this code below on a Razor view _hearder_it.cshtml.

I am doing a StreamReader on test.aspx page which has a sitemaster.master only. The req.GetResponse does return the stream from the sitemaster(menu bar etc.). However the sitemaster.master has Request.Cookies and the cookies never have a value. I know they should have a value because I already test outside of the mvc application. The cookie changes the view of the sitemaster and that is the reason I need it.

//This code does returns the stream .

WebRequest req = HttpWebRequest.Create(url );
req.Method = "GET";

string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))

source = reader.ReadToEnd();


Response.Write(source);  // I get HTML result back from my sitemaster.master.
tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

0

Cookies are sent in request headers, while you don't add any cookies to your webrequest here. Here is a post that might help you

Community
  • 1
  • 1
Laurent S.
  • 6,816
  • 2
  • 28
  • 40
0

I added the cookie in the CookieContainer. This code is working successfully.
This code is in Razor view _header_it.cshtml:

 @{
       string userTyp3 = Request.Cookies["MY_USERTYPE"] != null ? Server.UrlDecode(Request.Cookies["MY_USERTYPE"].Value) : "";

        CookieCollection _CookieCollection2  = new CookieCollection();

        HttpWebRequest _request2 = (HttpWebRequest)WebRequest.Create("http://MySite_TEST/it/test.aspx");
        _request2.Method = "GET";
        _request2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        _request2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
        _request2.Referer = "http://MySite_TEST/it";
        _request2.KeepAlive = true;

        //Here is auth cookie, works fine
        _request2.CookieContainer = _cookieContainer;
        _request2.CookieContainer.Add(( new Cookie ( "MY_USERTYPE", userTyp3 , "/", "MySite_TEST") )) ;
        _request2.Headers.Add(HttpRequestHeader.CacheControl, "no-cache=set-cookie");

        HttpWebResponse _response2 = (HttpWebResponse)_request2.GetResponse();
        StreamReader _reader2 = new StreamReader(_response2.GetResponseStream());
        Response.Write(_reader2.ReadToEnd());  // 

        _response2.Close();
        _reader2.Close();       

}

I used the example on this URL:

 http://stackoverflow.com/questions/2476092/login-website-curious-cookie-problem?rq=1]

Thank you