SharePoint WebClient返回401未经授权 [英] SharePoint WebClient Returning 401 unauthorized

查看:177
本文介绍了SharePoint WebClient返回401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个使用WebClient 

I developed a webpart that use WebClient 

的webpart,但我收到了401未经授权的错误

but I am getting 401 unauthorized error

我已经设置了使用默认凭据是真的

I have set the use default credential to be true

任何人都可以提供帮助

David

推荐答案

Hi Hunty,

Hi Hunty,

如果您使用WebClient类来调用SharePoint Rest API,您可以按照此演示,使用NewNetworkCredentials类传递用户名,密码,域到WebClient类:

If you are using WebClient class to call like SharePoint Rest API, you could follow this demo, use NewNetworkCredentials class to pass the username, password, domain into WebClient class:

SharePointAndRest_WebClient.cs:

SharePointAndRest_WebClient.cs:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class SharePointAndRest_WebClient:IDisposable
    {
        private readonly WebClient webClient;
        public Uri WebUri { get; private set; }

        /// <summary>
        /// Intialize webclient and set it up for use with sharepoint using Default Credentials
        /// </summary>
        /// <param name="webUri">Basic url to sharepoint site</param>
        public SharePointAndRest_WebClient(Uri webUri)
        {
            ICredentials credentials = new NetworkCredential("Administrator", "Access1", "Contoso");
            webClient = new WebClient();
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
            webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
            WebUri = webUri;
            webClient.Credentials = credentials;

        }

        public JToken GetListItems(string listTitle)
        {
            var endpointUri = new Uri(WebUri, string.Format("_api/web/lists/getbytitle('{0}')/items", listTitle));
            var result = webClient.DownloadString(endpointUri);
            var t = JToken.Parse(result);
            return t["d"]["results"];
        }

        /// <summary>
        /// IDisposable Implementation
        /// </summary>
        public void Dispose()
        {
            webClient.Dispose();
            GC.SuppressFinalize(this);
            GC.Collect();
        }
    }
}

这样调用:

 static void Main(string[] args)
        {
            SharepointAndRest_WebClient_Get();
        }
        

        /// <summary>
        /// This method gets the data from sharepoint list and prints it on console
        /// </summary>
        public static void SharepointAndRest_WebClient_Get()
        {
            var webUri = new Uri(@"http://sp/sites/dev/");
           

            using (var client = new SharePointAndRest_WebClient(webUri))
            {
                var x = client.GetListItems("MyList3");
                foreach (var item in x)
                {
                   
                    Console.Write(((string)item["Title"]));
              
                }
            }
        }





参考:

Sharepoint和REST - WebClient - 获取列表数据

如果想更新列表项,首先需要获取表单摘要值并传入WebClient.Headers:

And if want to update list item, need to get form digest value firstly and pass into WebClient.Headers:

Sharepoint和REST - WebClient - 将数据更新到列表

谢谢

最好的问候


这篇关于SharePoint WebClient返回401未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆