HttpWebRequest Sharepoint API身份验证FedAuth 401

时间:2017-04-05 14:33:33

标签: c# sharepoint httpwebrequest

我需要访问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'";

的情况下访问网址,此代码可以正常使用(http结果200)

我还尝试将httpWebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");添加到我的标题中。

如果我理解得很好,这个标题告诉Sharepoint服务器使用NTLM身份验证,这不是我想要的,因为我的身份验证方法基于FedAuth系统。

我想我错过了什么。

1 个答案:

答案 0 :(得分:0)

最后,

_trust/基本网址错误:/

使用正确的网址,它现在正在运行:)

此问题现在至少可以用作how to send FedAuth Cookie上的参考:)