将HttpClient响应转换为ASPX页面的HttpResponse重定向

时间:2019-03-19 16:16:07

标签: dotnet-httpclient .aspxauth

我们有一个大约2010年的Intranet网站,允许我们的用户从我们自己的数据库中搜索数据。从搜索中选择内容后,我们的网站会将用户定向到一个页面,该页面将向第三方外部网站验证我们用户的Windows凭据。后面的代码完成登录后,会将它们重定向到已通过身份验证的站点,以供进一步使用。

我们的第3方已升级了他们的网站,我正试图将我们的用户从我们的页面重定向到使用新的身份验证过程(以前是服务)现已登录的页面,现在是表单登录页面。

我可以使用HttpClient对象自动化登录并进行所有阶段的操作,但是我试图通过拉动cookie和标头并为最终URL提供重定向来将登录响应HttpResponseMessage移至aspx页面的HttpResponse。我的代码如下。

protected void Page_Load(object sender, EventArgs e)
{

        string loginurl = GetLoginUrlString();
        ServicePointManager.SecurityProtocol =
            SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

        CookieContainer cookies = new CookieContainer();
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;
        handler.UseCookies = true;
        var httpClient
            = new HttpClient(handler);

        string html = httpClient.GetStringAsync(loginurl).Result;
        var token = Regex.Match(html, "(?<=__RequestVerificationToken.*value=\")[a-zA-Z0-9-_]*").Value;
        HttpContent content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>( "email", GetLoginEmail() ),
            new KeyValuePair<string, string>( "Password", GetLoginPassword() ),
            new KeyValuePair<string, string>( "__RequestVerificationToken", token )
        });


        // authentication post
        HttpResponseMessage response = httpClient.PostAsync(loginurl, content).Result;
        string responseStuff = response.Content.ReadAsStringAsync().Result;

        var url = GetRedirectUriString();

        // this works but does not translate to 'THIS' aspx page.
        HttpResponseMessage myResponse = httpClient.GetAsync(url).Result;

        // the html page that we want the user to see (and be authenticated upon) 
        string redirectedStuff = myResponse.Content.ReadAsStringAsync().Result;


        // This is what I am really trying to do... 
        //I tried moving cookies and headers from the HttpResponseMessage
        // to this page's Response object, followed by a redirect to the final
        // URL but the authentication does not pass along
        foreach (Cookie cookie in cookies.GetCookies(GetCookieDomainUri()))
        {
            Response.Cookies.Add(new HttpCookie(cookie.Name, cookie.Value)
            { Domain = cookie.Domain, Expires = cookie.Expires });
        }


        // convert KeyValuePair<string, IEnumerable<string>> to NameValueCollection for Page.Headers
        foreach (var header in response.Headers)
        {
            // each header is a KeyValuePair<string, IEnumerable<string>>,
            // this.Response.Headers can only add a <string,string> or a NameValueCollection
            foreach (var element in header.Value.ToArray())
            {
                Response.Headers.Add(header.Key, element);
            }
        }
        // This response is sadly not authenticated and gets a redirect to the login we already did.
        Response.Redirect(url);

    }

那么我要去哪里错了,或者我想念什么?任何帮助表示赞赏。

0 个答案:

没有答案