HttpWebRequest的不及格证书简单的形式认证 [英] HttpWebRequest not passing Credentials simple form authentication

查看:151
本文介绍了HttpWebRequest的不及格证书简单的形式认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是这样

Doc doc = new Doc();
string url =  HttpContext.Current.Request.Url.AbsoluteUri;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// required for HttpWebResponse.Cookies
request.CookieContainer = new CookieContainer(); 

request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.Cookies.Count>0) { // includes ASP.NET_SessionId
    bool needsCookie2 = false;
    StringBuilder builder = new StringBuilder("Cookie: ");
    for(int i = 0; i<response.Cookies.Count; ++i) {
        Cookie cookie = response.Cookies[i];
        if(!needsCookie2 && cookie.Version!=1)
             needsCookie2 = true;
        if(i>0)
             builder.Append("; ");
        builder.Append(cookie.ToString());
    }
    builder.Append(!needsCookie2? "\r\n": "\r\nCookie2: $Version=1\r\n");
    doc.HtmlOptions.HttpAdditionalHeaders = builder.ToString();
}

doc.HtmlOptions.NoCookie = true;
doc.HtmlOptions.HostWebBrowser = false;

// cookieless Forms Authentication adds authentication ticket to the URL
int id = doc.AddImageUrl(response.ResponseUri.AbsoluteUri);
doc.Save(HttpContext.Current.Server.MapPath("~/media/pdf/1212.pdf"));
doc.Clear();

我使用的简单的表单验证在我的网站,我需要以打印通过abcPDF一个PDF验证该WebRequest的,因为我的登录信息都存储在cookie的我试图得到它那里,添加到我的请求。我假设的错误是在行 request.Credentials =新的NetworkCredential(用户名,密码);

I am using simple form authentication in my website and I need to authenticate this webRequest in order to print a pdf through abcPDF, as my login details are stored in cookie i am trying to get it from there and add to my request. I assume the error is in line request.Credentials = new NetworkCredential("username", "password");

推荐答案

您说的网站使用的窗体身份验证的,但你的请求正在使用的基本身份验证的凭据:

You say the website is using forms authentication, but your request is using basic authentication credentials:

request.Credentials = new NetworkCredential("username", "password");

您需要的网站或者切换到基本身份验证,或对你的登录页面执行POST请求来获取会话cookie /令牌在后续请求中使用。

You'll need to either switch the website to basic authentication, or perform a POST request against your login page to get the session cookie/token to use in subsequent requests.

这篇关于HttpWebRequest的不及格证书简单的形式认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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