2011-09-08 67 views
1

我有一个HttpWebRequest来获取会话ID。然后,我从响应中获取cookie,将其添加到第二个请求以获取我需要的页面。为什么我的HttpWebRequest重定向到登录页面?

使用IIS/7.5,这种失败有哪些可能的情况? 我正在使用Fiddler,并获得302状态。我正在获取ASPNET SessionID。

CookieContainer myCookies = new CookieContainer(); 
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://www.secure.com/login.aspx"); 
    req.Method = "POST"; 
    req.Credentials = fileretrieve.Credentials;//Network credentials. 
    req.CookieContainer = myCookies; 
    req.AllowAutoRedirect = true; 

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(req.Credentials.ToString()); 
    req.ContentLength = bytes.Length; 
    System.IO.Stream os = req.GetRequestStream(); 
    os.Write(bytes, 0, bytes.Length); 
    os.Close(); 

    HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 

    HttpWebRequest xmlreq = (HttpWebRequest)HttpWebRequest.Create("https://www.secure.com/file"); 
    xmlreq.Method = "GET"; 
    xmlreq.KeepAlive = true; 
    xmlreq.AllowAutoRedirect = true; 
    xmlreq.CookieContainer = req.CookieContainer; 


    HttpWebResponse xmlresp = (HttpWebResponse)xmlreq.GetResponse(); 

    string webpage; 

    System.IO.StreamReader sr = new System.IO.StreamReader(xmlresp.GetResponseStream()); 
    webpage = sr.ReadToEnd();      
+2

没有显示代码,很难提供帮助。 – Icarus

+0

几个可能的场景 - 有一个HTTP模块运行一些代码,强制在代码之外重定向。检查您的Global.asax代码处理请求是否与您期望的不同。 – EtherDragon

回答

0

我认为这是失败的,因为你要使用.Credentials属性基于FormsAuthentication的应用程序进行身份验证。 Credentials属性可用于Basic或NTLM身份验证,但不适用于Forms身份验证,在这种情况下,您只需将预期字段POST到Login.aspx页面即可获取Auth cookie。

+0

我加了:string cred =“username =”+用户名+“&password =”+密码; byte [] bytes = System.Text.Encoding.ASCII.GetBytes(cred.ToString());]' – ksmuck

+0

没有那个运气。 – ksmuck