2017-04-05 38 views
0

我需要访问SharePoint _api /网络/列表API。 我有一个FedAuth cookie,我将注入我的HttpWebRequest。 这是我的代码的HttpWebRequest的SharePoint API认证FedAuth 401

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

此代码工作正常(HTTP结果200),如果我想访问URL没有@"_api/Web/Lists/GetByTitle('Title')/items?$filter=SomeKey eq 'SomeValue'";

我也尝试添加httpWebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");我头。

如果我深知,这个头告诉SharePoint服务器使用NTLM身份验证,这不是我想要的,因为我的身份验证方法是基于FedAuth SYSTEME。

我想我失去了一些东西。

+0

我刚才添加的httpWebRequest.Headers.Add( “X-FORMS_BASED_AUTH_ACCEPTED”, “F”);我的要求,我没有相同的标题答案了,但我仍然有401错误:/ – pix

回答

0

最后,

_trust/基本URL是错误的:/

正确的URL它现在的工作:)

这个问题,现在至少可以作为一个参考how to send FedAuth Cookie :)