针对sharepoint 2010的Rest API身份验证

时间:2018-01-22 13:02:27

标签: rest api sharepoint sharepoint-2010

如何对Sharepoint 2010的其余API调用进行身份验证和授权。 另请提供有关如何使用邮递员休息客户端验证其余API调用的步骤。

1 个答案:

答案 0 :(得分:0)

如果您想使用C#授权其余API,这里有一个简单的演示,通过使用C#的REST API检索列表数据供您参考:

    /// <summary>
    /// retrieve JSON Data of list via REST API
    /// </summary>
    /// <param name="webUri"></param>
    /// <param name="credentials"></param>
    /// <param name="listTitle"></param>
    /// <returns></returns>
    public static JToken GetList(Uri webUri, ICredentials credentials, string listTitle)
    {
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.Credentials = credentials;
            client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
            client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
            Uri endpointUri = new Uri(webUri + "/_api/web/lists/getbytitle('" + listTitle + "')");
            string result = client.DownloadString(endpointUri);

            JObject jo = (JObject)JsonConvert.DeserializeObject(result);
            JToken jToken = jo["d"];
            return jToken;
        }
    }

关于凭证,我们可以执行以下操作。

 NetworkCredential credentials= new NetworkCredential(userName, password, domain);