Using HTTP Authentication with a C# WebRequest

微笑、不失礼 提交于 2019-11-27 06:34:21

问题


I want to make a web request to a page that needs authenticating. How would I go about doing this? I found something that said possibly to use the Credentials property, but I'm not sure how to use it.


回答1:


Assign a new NetworkCredential instance to the Credentials property:

webClient.Credentials = new NetworkCredential("Mehrdad", "Password");



回答2:


Basic auth example:

public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
    string authInfo = userName + ":" + userPassword;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
}

http://blog.kowalczyk.info/article/at3/Forcing-basic-http-authentication-for-HttpWebReq.html




回答3:


It is also possible to authenticate automatically with. This will use the credentials of the currently logged on user.

webClient.Credentials = CredentialCache.DefaultCredentials


来源:https://stackoverflow.com/questions/707888/using-http-authentication-with-a-c-sharp-webrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!