[英]How can I access SharePoint 2010 site page which has claim based authentication through windows application?
我需要创建一个Windows应用程序,该应用程序将访问SharePoint 2010网站页面,以“ .htm”格式将该页面添加到本地驱动器中。
但是,该SharePoint网站具有基于声明的身份验证。
为了访问该站点,我必须提供“用户名”和“密码”以及“域名”。
因此,我需要有关如何通过Windows应用程序传递站点凭据才能访问该站点的帮助??
我已使用以下代码,但是会引发异常“远程服务器返回错误:(403)禁止”。
WebRequest Request1;
HttpWebResponse Response1;
Request1 = WebRequest.Create(txtUrl.Text.ToString());
Request1.Credentials = new NetworkCredential(strUserNm.ToString(), strPassword.ToString(), StrDomain.ToString());
Request1.PreAuthenticate = true;
Response1 = (HttpWebResponse)Request1.GetResponse();
任何帮助将是可观的。
基于具有双重身份验证模式的Warmup计时器作业 ,我对此进行了更改:
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
WebResponse response = request.GetResponse();
对此:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = new CredentialCache
{
{ new Uri(url), "NTLM", new NetworkCredential(username, password, domain) }
};
request.Headers.Add("Authorization", GetAuthorization(username, password, domain));
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = new CookieContainer();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
GetAuthorization方法如下所示:
private function GetAuthorization(string username, string password, string domain)
{
string credentials = string.Format(@"{0}\{1}:{2}", domain, username, password);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
return = string.Concat("NTLM ", base64);
}
使用更新的代码,我可以访问“经典”和“基于声明”的Web应用程序中的文件。
请注意如何捕获域。
//请勿将domain \\ username或username @ domain用作此构造函数的username参数。 (根据设计)这将不起作用。 采用域的构造函数具有3个参数。 或使用上面的示例代码中明确记录的domain属性。
// Call the onstructor to create an instance of NetworkCredential with the
// specified user name and password.
NetworkCredential myCredentials = new NetworkCredential(username,passwd);
// Create a WebRequest with the specified URL.
WebRequest myWebRequest = WebRequest.Create(url);
myCredentials.Domain = domain;
myWebRequest.Credentials = myCredentials;
Console.WriteLine("\n\nCredentials Domain : {0} , UserName : {1} , Password : {2}",
myCredentials.Domain, myCredentials.UserName, myCredentials.Password);
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...");
// Send the request and wait for a response.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Process the response.
Console.WriteLine("\nResponse received successfully.");
// Release the resources of the response object.
myWebResponse.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.