编程语言
首页 > 编程语言> > C#-HTTPS的HttpWebRequest收到403禁止错误

C#-HTTPS的HttpWebRequest收到403禁止错误

作者:互联网

我从C#项目中调用GetListCollection方法时收到错误消息“ 403 Forbidden”.在线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

解决方法:

由于无法在Office 365中利用NetworkCredential class,因此会发生此错误,Microsoft支持claims-based authentication in Office 365.

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

如何在Office 365中使用SharePoint Web Services

下面的示例演示如何在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);
 }

标签:office365,c,soap
来源: https://codeday.me/bug/20191028/1953102.html