c# - Cookie/授权未从 POST 延续到 GET
问题描述
我已经开始使用 Postman 来测试我的请求,现在我可以进行身份验证并获取我正在寻找的数据。当我尝试在 C# 中做同样的事情时,我没有那么幸运。我已经明白,只有在仍然需要授权或授权失败时,授权标头“X-com-ibm-team-repository-web-auth-msg”才会存在。由于在我尝试使用“_formPost”进行身份验证后它现在消失了,我很确定它是成功的。但是,当我执行“请求”时,我再次获得带有 authrequired 的标题。所以它没有延续。我想我需要用饼干做更多的事情。我需要做什么?
更新:显然问题是重用我的 HttpWebRequest “请求”。为什么我不能重复使用它?为什么我必须制作一个新的副本?
// All the real strings are trustworthy as compared with Postman which works.
string basicCredentials = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(un + ":" + pw));
string formCredentials = "j_username=" + un + "&j_password=" + pw;
string host = "https://my.host.com/ccm/";
string itemUrl = host + "oslc/contexts/_mYsp3ci4lK3y/workitems?stuff"
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(item);
request.Headers.Add("Authorization", basicCredentials);
request.CookieContainer = new CookieContainer(); // Do I need anything more here?
request.Headers.Add("OSLC-Core-Version", "2.0");
request.Accept = "application/rdf+xml";
request.Method = "GET";
WebResponse response = request.GetResponse();
string AuthHeader = response.Headers["X-com-ibm-team-repository-web-auth-msg"];
if (AuthHeader.Equals("authrequired")) // Always does
{
// Now to authenticate with form authentication
HttpWebRequest _formPost = (HttpWebRequest)WebRequest.Create(host + "j_security_check"); // Confirmed with Postman
_formPost.Method = "POST";
_formPost.Headers.Add("OSLC-Core-Version", "2.0");
_formPost.UserAgent = "PostmanHadOneSoIPopulatedThis"; // No clue if necessary
_formPost.Accept = "text/xml";
_formPost.ContentType = "application/x-www-form-urlencoded";
// Setting the cookie container to the request's container hoping
// it would carry over the authorization. Does this get populated on success?
_formPost.CookieContainer = request.CookieContainer;
// Oddly enough, I must have basic authorization credentials in order to post my form credentials.
// Otherwise, without this, it continues to say "authrequired" in the header
_formPost.Headers.Add("Authorization", basicCredentials);
// This must be all good. If/When I mess it up, I get the header "authfailed".
Byte[] _outBuffer = Encoding.UTF8.GetBytes(formCredentials);
_formPost.ContentLength = _outBuffer.Length;
Stream _str = _formPost.GetRequestStream();
_str.Write(_outBuffer, 0, _outBuffer.Length);
_str.Close();
HttpWebResponse _formResponse = (HttpWebResponse)_formPost.GetResponse();
string _rtcAuthHeader = _formResponse.Headers["X-com-ibm-team-repository-web-auth-msg"];
// Always null now which means it passed authentication
if (_rtcAuthHeader == null)
{
// Trying the request again
response = (HttpWebResponse)request.GetResponse();
// Updating header to check authorization
_rtcAuthHeader = response.Headers["X-com-ibm-team-repository-web-auth-msg"];
// _rtcAuthHeader always == authrequired
// Did not retain authorization.
}
}
解决方案
显然它不喜欢我重复使用“请求”。我必须用所有相同的数据创建一个“NewRequest”,然后再试一次。不知道为什么。如果您知道,请随时分享,但这解决了我的问题。
推荐阅读
- python - 如何根据索引合并两个 numpy 数据集?
- python - 以一组元组为值的字典理解
- python - 从python中的多个链接下载并将其存储在多个列表中
- c# - 如何等待表为空/消息队列已处理?
- c - strsep() 导致段错误 - 为什么?
- java - 将鼠标悬停在其方法和类上时如何显示导入库的 Javadoc?
- python - 如何使用条件从多维 numpy 数组中删除子数组?
- python - 用rabbitmq在数据库上写
- delegates - 在 BPMN 中将 Java Delegate 与 Delegate Expression 一起使用时,表达式中使用了未知属性
- python - 通过比较子数组将numpy数组转换为方数组