Cookie /授权未从POST传递到GET

时间:2020-04-10 18:25:12

标签: c# cookies httpwebrequest forms-authentication

我已经开始使用Postman来测试我的请求,现在我可以进行身份​​验证并获取所需的数据。当我尝试在C#中做同样的事情时,我没有那么幸运。我了解到,只有在仍然需要授权或授权失败的情况下,授权标题“ X-com-ibm-team-repository-web-auth-msg”才会存在。由于在我尝试使用“ _formPost”进行身份验证后,该密码现在消失了,因此我可以肯定它成功了。但是,当我执行“请求”时,我再次获得带有authrequired的标头。所以它没有延续。我想我需要对Cookie做更多的事情。我该怎么办?

更新:显然问题出在重用我的HttpWebRequest“ request”。我为什么不能重复使用它?为什么我必须重新制作一份副本?

// 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.
    }

}

1 个答案:

答案 0 :(得分:0)

显然,我不喜欢我重复使用“请求”。我必须使用所有相同的数据创建一个“ NewRequest”,然后重试。不知道为什么。如果您知道,随时分享,但这解决了我的问题。

相关问题