用于HTTPS的HttpWebRequest获得403 Forbidden Error

时间:2015-03-27 04:19:09

标签: c# soap office365

我收到错误消息" 403 Forbidden"从我的C#项目调用GetListCollection方法。在线2013和URL中的sharepoint版本以HTTPS开头。

我的HTTP标头请求的代码是:

string _url = "https://my.sharepoint.com/"+ "_vti_bin/Lists.asmx";

string soapStr =
            @"<?xml version=""1.0"" encoding=""utf-8""?>
                <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <soap:Body>
                        <GetListCollection xmlns=""http://schemas.microsoft.com/sharepoint/soap/"" />
                    </soap:Body>
                </soap:Envelope>";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_url);

req.Headers.Add("SOAPAction", "\"http://schemas.microsoft.com/sharepoint/soap/GetListCollection\"");
req.ContentType = "text/xml; encoding='utf-8'";
req.Credentials = new NetworkCredential(userName, userPassword);
req.Method = "POST";

            using (Stream stm = req.GetRequestStream())
            {
                soapStr = string.Format(soapStr);
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soapStr);
                }
            }
            StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream());

有人可以告诉我我错在哪里或者我需要做什么吗?

在提琴手中,我收到了以下回复。

HTTP/1.1 403 Forbidden
Cache-Control: private, max-age=0
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.5
X-SharePointHealthScore: 0
X-Forms_Based_Auth_Required: https://my.sharepoint.com/_forms/default.aspx?ReturnUrl=/_layouts/15/error.aspx&Source=%2f_vti_bin%2fLists.asmx
X-Forms_Based_Auth_Return_Url: https://my.sharepoint.com/_layouts/15/error.aspx
X-MSDAVEXT_Error: 917656; %e3%82%a2%e3%82%af%e3%82%bb%e3%82%b9%e3%81%8c%e6%8b%92%e5%90%a6%e3%81%95%e3%82%8c%e3%81%be%e3%81%97%e3%81%9f%e3%80%82%e3%81%93%e3%81%ae%e5%a0%b4%e6%89%80%e3%81%ae%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e9%96%8b%e3%81%8f%e5%89%8d%e3%81%ab%e3%80%81Web+%e3%82%b5%e3%82%a4%e3%83%88%e3%82%92%e5%8f%82%e7%85%a7%e3%81%97%e3%81%a6%e3%80%81%e8%87%aa%e5%8b%95%e7%9a%84%e3%81%ab%e3%83%ad%e3%82%b0%e3%82%a4%e3%83%b3%e3%81%99%e3%82%8b%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92%e9%81%b8%e6%8a%9e%e3%81%97%e3%81%a6%e3%81%8f%e3%81%a0%e3%81%95%e3%81%84%e3%80%82
X-AspNet-Version: 4.0.30319
SPRequestGuid: 7dbaf69c-a0f4-1000-ba3c-f5906d15f5d7
request-id: 7dbaf69c-a0f4-1000-ba3c-f5906d15f5d7
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 66
SPIisLatency: 0
X-IDCRL_AUTH_PARAMS_V1: IDCRL Type="BPOSIDCRL", EndPoint="/_vti_bin/idcrl.svc/", RootDomain="sharepoint.com", Policy="MBI"
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 16.0.0.3819
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
Date: Fri, 27 Mar 2015 04:26:16 GMT
Content-Length: 13

403 FORBIDDEN

2 个答案:

答案 0 :(得分:3)

发生此错误,因为NetworkCredential class可能 在Office 365中使用,Microsoft支持claims-based authentication in Office 365

SharePoint Online Client Components SDK已发布,其中包含
SharePointOnlineCredentials class访问SharePoint Online资源。

如何在Office 365中使用SharePoint Web服务

以下示例演示了如何在Office 365中验证请求:

string endpointUrl = webUri + "/_vti_bin/Lists.asmx";
var envelope = 
                  "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                  "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                    "<soap:Body>" +
                       "<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />" +
                    "</soap:Body>" +
                  "</soap:Envelope>";


var request = (HttpWebRequest) WebRequest.Create(endpointUrl);
request.ContentType = "text/xml; encoding='utf-8'";
request.Credentials = GetCredentials(userName, password);
request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
request.Method = "POST";
using (var requestStream = request.GetRequestStream())
{
    using (var streamWriter = new StreamWriter(requestStream))
    {
       streamWriter.Write(envelope);
    }
}


using (var response = request.GetResponse())
{
     using (var responseStream = response.GetResponseStream())
     {
        var streamReader = new StreamReader(responseStream);
        var data = streamReader.ReadToEnd();
     }         
}

,其中

 public static SharePointOnlineCredentials GetCredentials(string userName, string password)
 {
     var securePassword = new SecureString();
     foreach (var ch in password) securePassword.AppendChar(ch);
     return  new SharePointOnlineCredentials(userName, securePassword);
 }

答案 1 :(得分:1)

403 Forbidden是尝试通过HTTPS访问服务而未在客户端正确设置证书时接收的典型错误代码。话虽如此,问题可能出在多个地方,也取决于您尝试联系的服务器上如何设置身份验证。

我认为this article将有助于启动。然后看this thread,因为讨论指出了可能对您有所帮助的几个问题和想法。