HttpWebRequest Sharepoint API 身份验证 FedAuth 401

HttpWebRequest Sharepoint API Authentication FedAuth 401

我需要访问 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 有以下 header 错误:

((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"); 添加到我的 header 中。

如果我没看错的话,这个header告诉Sharepoint服务器使用NTLM认证,这不是我想要的,因为我的认证方法是基于FedAuth系统的。

我想我错过了什么。

最后,

_trust/ 基础 url 是错误的:/

有了正确的 URL 它现在可以工作了:)

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