且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何登录窗口授权

更新时间:2023-12-04 10:22:52

如果目标页面使用Windows身份验证,则很容易:



  //  我们将使用WebClient类来阅读网页的HTML  
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = new NetworkCredential( myusername mypassword);

// 读取网页HTML到字节数组
Byte [] PageHTMLBytes;
...





这会将Windows凭据添加到身份验证标题中。



如果使用其他身份验证机制,则需要以不同方式提供凭据。表格认证的一个很好的例子可以在以下网址找到:

c# - WebClient访问带有凭据的页面 - Stack Overflow [ ^ ]


I have probelem guys i am taking HMTL code from my local Site i am using this code

         // We'll use WebClient class for reading HTML of web page
WebClient MyWebClient = new WebClient();

// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (textBox1.Text != "")
{

  PageHTMLBytes = MyWebClient.DownloadData(textBox1.Text);

  // Convert result from byte array to string
  // and display it in TextBox txtPageHTML
  UTF8Encoding oUTF8 = new UTF8Encoding();
  richTextBox1.Text = oUTF8.GetString(PageHTMLBytes);
}


buut this site first need Windows Authoristation i know User and password buuut how can i authorise with form it is erroring mee unauthorise beaocuse i don't enter any user and password i need that form that will take this password in password field and user in user field Help guys

If the target page is using Windows authentication it's easy:

         // We'll use WebClient class for reading HTML of web page
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = new NetworkCredential("myusername","mypassword");
 
// Read web page HTML to byte array
Byte[] PageHTMLBytes;
...



This will add the windows credentials into the authentication header.

If a different authentication mechanism is being used, you will need to provide the credentials in a different way. A really good example for forms authentication can be found at:
c# - WebClient accessing page with credentials - Stack Overflow[^]