C# 如何从重定向到login.microsoft的网站获取html代表?

C# 如何从重定向到login.microsoft的网站获取html代表?,c#,authentication,httprequest,single-sign-on,windows-authentication,C#,Authentication,Httprequest,Single Sign On,Windows Authentication,我使用blow代码通过URL-into变量strOutput Dim strURL As String = "https://example.net/exmplepage.aspx" Dim strOutput As String = "" Dim wrResponse As WebResponse Dim wrRequest As WebRequest = HttpWebRequest.Create(strURL) wrRequest.Credentia

我使用blow代码通过URL-into变量
strOutput

Dim strURL As String = "https://example.net/exmplepage.aspx"
Dim strOutput As String = ""
Dim wrResponse As WebResponse
Dim wrRequest As WebRequest = HttpWebRequest.Create(strURL)
wrRequest.Credentials = CredentialCache.DefaultNetworkCredentials
wrResponse = wrRequest.GetResponse()           
Dim stream As Stream = wrResponse.GetResponseStream()

Using sr As New StreamReader(stream)
 strOutput = sr.ReadToEnd()
 sr.Close()
End Using
不幸的是,网站有一些变化。现在,当我通过浏览器访问URL时,它首先重定向到,几秒钟后自动重定向回正确的URL。但是,当我尝试使用该代码时,我会将Microsoft页面的HTML放入
strOutput

Dim strURL As String = "https://example.net/exmplepage.aspx"
Dim strOutput As String = ""
Dim wrResponse As WebResponse
Dim wrRequest As WebRequest = HttpWebRequest.Create(strURL)
wrRequest.Credentials = CredentialCache.DefaultNetworkCredentials
wrResponse = wrRequest.GetResponse()           
Dim stream As Stream = wrResponse.GetResponseStream()

Using sr As New StreamReader(stream)
 strOutput = sr.ReadToEnd()
 sr.Close()
End Using

在上面的屏幕截图中,我以匿名模式打开了URL,因此它卡在了Microsoft登录页面上,这就是我的httprequest返回的内容。如果我在浏览器中正常打开URL,它会自动进行身份验证并重定向到正确的页面

当然,我将如何修改我的代码,以便它获取正确的html响应

更新

我能够使用另一个肮脏的解决方法来避免这个问题。
我将url加载到webbrowser控件中,然后获取该控件的文档文本。这为我提供了目标页面所需的html

这不是关于Windows身份验证,而是关于单点登录(oAuth)。您的webclient(HttpRequest)未经身份验证,因此您可以添加所有您想要的DefaultNetworkCredentials,但这对您没有帮助。很明显,您尝试访问的网页受此SSO保护-因此您的客户端应该在您阅读该网页之前进行身份验证。是否可以在代码中进行身份验证?是的,谷歌会为您的特定情况提供SAML、SSO身份验证等关键字。这不是一个答案和/或一条评论可以解释的。