从写作的Cookie的CookieContainer的IE浏览器cookie存储 [英] Writing cookies from CookieContainer to the IE cookie store

查看:176
本文介绍了从写作的Cookie的CookieContainer的IE浏览器cookie存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想浏览的页面从一个桌面应用程序的Web应用程序。 没问题,我听到你说,刚刚火了正确的URL的默认浏览器。然而,Web应用程序使用ASP.NET窗体身份验证,而用户不希望看到的登录页面,因为他们已经与桌面应用程序相同的凭据进行身份验证。

这听起来很简单,我所要做的就是从发射与桌面应用程序的HTTP POST假货从Web应用程序的登录页面的回发。该网站的应用程序将然后将其身份验证票证和会话状态的cookie,他们还给我,我将它们存储在IE浏览器cookie存储。那么我可以导航到所需的网页和Web应用程序会认为它已经通过身份验证。

我有一定的工作code,它构建了HTTP POST,将其关闭,并获得包含正确的饼干有效的响应。不过,我看不出如何将它们写入到IE浏览器的cookie存储。任何人都可以点我朝着正确的方向?

样品code:

  VAR requestUrl = Properties.Settings.Default.WebsiteLoginPageUrl;

VAR requestEncoding = Encoding.GetEncoding(1252);

//模拟登录POSTDATA
VAR requestText =的String.Format(
    "__VIEWSTATE={2}&__EVENTTARGET={3}&__EVENTARGUMENT={4}&__EVENTVALIDATION={5}&userNameText={0}&passwordText={1}&submitButton=Log+In",
    HttpUtility.UrlEn code(Properties.Settings.Default.UserName)
    HttpUtility.UrlEn code(Properties.Settings.Default.Password)
    Properties.Settings.Default.FakeViewState,
    Properties.Settings.Default.FakeEventTarget,
    Properties.Settings.Default.FakeEventArgument,
    Properties.Settings.Default.FakeEventValidation);

VAR请求=(HttpWebRequest的)WebRequest.Create(requestUrl);
request.Method =POST;
request.Accept =* / *;
request.ContentType =应用/的X WWW的形式urlen codeD;
request.ContentLength = requestEncoding.GetByteCount(requestText);
request.Headers.Add(Htt的prequestHeader.CacheControl,无缓存);
request.AllowAutoRedirect = FALSE;
request.KeepAlive = FALSE;
request.CookieContainer =新的CookieContainer();

使用(VAR作家=新的StreamWriter(request.GetRequestStream(),requestEncoding)){
    writer.Write(requestText);
}

变种响应=(HttpWebResponse)request.GetResponse();

// TODO:抓住响应cookie,并将它们保存到交互式桌面用户的cookie存储。

的Process.Start(新的ProcessStartInfo {
    文件名= Properties.Settings.Default.WebsiteTargetPageUrl,
    UseShellExecute = TRUE,
});
 

解决方案

您需要调用非托管的 InternetSetCookie()功能。再看看!有人写的互操作你了。你应该不过验证其正确性。

I want to navigate to a page in a web app from a desktop app. "No problem", I hear you say, "just fire up the default browser with the correct URL". However, the web app uses ASP.NET Forms Authentication, and the users don't want to see the login page because they have already authenticated with the same credentials in the desktop app.

That sounds simple enough, all I have to do is emit an HTTP POST from the desktop app with that fakes the postback from the web app's login page. The web app will then set its authentication ticket and session state cookies, return them to me, and I will store them in the IE cookie store. I can then navigate to the desired page and the web app will think that it's already authenticated.

I have some working code that constructs the HTTP POST, sends it off, and gets a valid response containing the right cookies. However, I can't see how to write them into the IE cookie store. Can anyone point me in the right direction?

Sample code:

var requestUrl = Properties.Settings.Default.WebsiteLoginPageUrl;

var requestEncoding = Encoding.GetEncoding(1252);

// Simulated login postdata
var requestText = string.Format(
    "__VIEWSTATE={2}&__EVENTTARGET={3}&__EVENTARGUMENT={4}&__EVENTVALIDATION={5}&userNameText={0}&passwordText={1}&submitButton=Log+In",
    HttpUtility.UrlEncode(Properties.Settings.Default.UserName),
    HttpUtility.UrlEncode(Properties.Settings.Default.Password),
    Properties.Settings.Default.FakeViewState,
    Properties.Settings.Default.FakeEventTarget,
    Properties.Settings.Default.FakeEventArgument,
    Properties.Settings.Default.FakeEventValidation);

var request = (HttpWebRequest) WebRequest.Create(requestUrl);
request.Method = "POST";
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestEncoding.GetByteCount(requestText);
request.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
request.AllowAutoRedirect = false;
request.KeepAlive = false;
request.CookieContainer = new CookieContainer();

using(var writer = new StreamWriter(request.GetRequestStream(), requestEncoding)) {
    writer.Write(requestText);
}

var response = (HttpWebResponse) request.GetResponse();

// TODO: Grab the response cookies and save them to the interactive desktop user's cookie store.

Process.Start(new ProcessStartInfo {
    FileName = Properties.Settings.Default.WebsiteTargetPageUrl,
    UseShellExecute = true,
});

解决方案

You need to call the unmanaged InternetSetCookie() function. And look! Someone wrote the interop for you already. You should verify its correctness though.

这篇关于从写作的Cookie的CookieContainer的IE浏览器cookie存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆