【发布时间】:2017-07-13 16:11:39
【问题描述】:
我需要访问 Sharepoint _api/Web/Lists api。
我有一个 FedAuth cookie,我将把它注入我的 HttpWebRequest。
这是我的代码
ServicePointManager.ServerCertificateValidationCallback = (RemoteCertificateValidationCallback)Delegate.Combine(ServicePointManager.ServerCertificateValidationCallback, new RemoteCertificateValidationCallback((object SearchOption, X509Certificate cert, X509Chain chain, SslPolicyErrors sslerror) => true));
var requestUri = this._gedBaseUrl + @"_api/Web/Lists/GetByTitle('Title')/items?$filter=SomeKey eq 'SomeValue'";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.UserAgent = @"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
httpWebRequest.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.Accept = @"text/html,application/xhtml+xml,*/*";
httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.CookieContainer.Add(cookie);
try
{
HttpWebResponse endpointResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception e)
{
throw e;
}
我有一个 WebException 带有以下标头错误:
((WebException)e).Response.Headers["X-MSDAVEXT_Error"] "917656; Access denied. Before opening files in this location, you must first browse to the web site and select the option to login automatically."
所以我决定查看我的浏览器的 cookie,我发送的是相同的东西 UserAgent 和 FedAuth 值。
所以我被 http 结果 401 卡住了。
如果我想在没有@"_api/Web/Lists/GetByTitle('Title')/items?$filter=SomeKey eq 'SomeValue'"; 的情况下访问 URL,此代码可以正常工作(http 结果 200)
我还尝试将httpWebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); 添加到我的标题中。
如果我理解得很好,这个标头告诉 Sharepoint 服务器使用 NTLM 身份验证,这不是我想要的,因为我的身份验证方法是基于 FedAuth 系统的。
我想我错过了什么。
【问题讨论】:
-
我只是添加了 httpWebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");根据我的要求,我不再有相同的标题答案,但我仍然有 401 错误:/
标签: c# sharepoint httpwebrequest