【问题标题】:I am trying to access sharepoint via C# code and i am getting UNAUTHORIZED access error but I can log web sharepoint without any issues我正在尝试通过 C# 代码访问共享点,但我收到未经授权的访问错误,但我可以毫无问题地记录网络共享点
【发布时间】:2019-09-23 21:47:06
【问题描述】:

我正在开发 C#.net 应用程序,该应用程序将使用客户端对象模型方法与 Sharepoint 2013 交互以下载和上传文件。当我使用我的凭据运行 C#.net 应用程序时,它给了我以下错误。

  • $exception {"远程服务器返回错误:(401) Unauthorized."} System.Net.WebException

但是,如果我使用相同的凭据通过 Web Sharepoint 登录,它就可以正常工作。我不明白我需要在我的代码中插入什么才能访问 SharePoint 以下载/上传文件。

任何建议都会有所帮助。提前致谢!

   public void test()
    {
        Console.WriteLine("SharePoint Online site URL:");

        try
        {
            using (var context = new ClientContext(webSPOUrl))
            {
                Web web = context.Web;
                context.Load(web, website => website.Title);
                context.Load(web.Webs);

                CredentialCache cc = new System.Net.CredentialCache();
                cc.Add(new Uri(webSPOUrl), "NTLM", CredentialCache.DefaultNetworkCredentials);
                context.Credentials = cc;
                context.AuthenticationMode = ClientAuthenticationMode.Default;

                context.Load(web.Lists,
                    lists => lists.Include(list => list.Title,
                        list => list.Id));


                context.ExecuteQuery();
                Console.ForegroundColor = ConsoleColor.White;
                foreach (List list in web.Lists)
                {
                    Console.WriteLine("List title is: " + list.Title);
                }
                Console.WriteLine("");

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error is: " + ex.Message);
        }
    }

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    谢谢大家,谁提供了解决方案。以下是工作代码:

    public static void Uploadfiles(string SiteUrl, string DocLibrary, string ClientSubFolder, string FileName)
            {
    
                string webSPOUrl = "https://testdev.sharepoint.com/sites/test";
                Console.WriteLine("User Name:");
                string userName = "<your username >@testdev.onmicrosoft.com";
                Console.WriteLine("Password:");
                string librayName = "Documents";
    
    
                    try
                    {
                        using (var context = new SP.ClientContext(webSPOUrl))
                        {
                            // Sharepoint Online Authentication
                            context.Credentials = new SP.SharePointOnlineCredentials(userName, password);
                            SP.Web web = context.Web;
    
                            SP.FileCreationInformation newFile = new SP.FileCreationInformation();
                            byte[] FileContent = System.IO.File.ReadAllBytes(FileName);
                            newFile.ContentStream = new System.IO.MemoryStream(FileContent);
                            newFile.Url = Path.GetFileName(FileName);
                            SP.List DocumentLibrary = web.Lists.GetByTitle(DocLibrary);
    
                            SP.Folder Clientfolder = DocumentLibrary.RootFolder.Folders.Add(ClientSubFolder);
    
                            Clientfolder.Update();
                            SP.File uploadFile = Clientfolder.Files.Add(newFile);
    
                            context.Load(DocumentLibrary);
                            context.Load(uploadFile);
                            context.ExecuteQuery();
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("The File has been uploaded" + Environment.NewLine + "FileUrl -->" + SiteUrl + "/" + DocLibrary + "/" + ClientSubFolder + "/" + Path.GetFileName(FileName));
                         } }
    

    【讨论】:

      【解决方案2】:

      根据您提供的代码 sn-p,您似乎正在尝试连接到 SharePoint Online (O365)。您在代码中使用的网络凭据对象用于连接到传统的“本地”SharePoint 安装。你应该改用SharePointOnlineCredentials

      context.Credentials = new SharePointOnlineCredentials(userName,password);
      context.Load(...);
      context.ExecuteQuery();
      

      【讨论】:

        【解决方案3】:

        如果您可以提供用户名、密码和域,此代码对我有用:

        var clientContext = new ClientContext(new Uri(someUrl))
        {
            Credentials = new System.Net.NetworkCredential("UserName","Password", "DomainName")
        }
        

        【讨论】:

          【解决方案4】:

          如果您想在客户端访问本地 SharePoint 2013,您可以修改如下代码。

          public void test()
          {
              try
              {
                  using (var context = new ClientContext(webSPOUrl))
                  {
                      context.Credentials = new NetworkCredential("username", "password", "domain");
                      Web web = context.Web;
                      context.Load(web, website => website.Title);
                      context.Load(web.Webs);
                      context.Load(web.Lists,
                          lists => lists.Include(list => list.Title,
                              list => list.Id));
          
                      context.ExecuteQuery();
                      Console.ForegroundColor = ConsoleColor.White;
                      foreach (List list in web.Lists)
                      {
                          Console.WriteLine("List title is: " + list.Title);
                      }
                      Console.WriteLine("");
          
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Error is: " + ex.Message);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-15
            • 2012-07-10
            • 1970-01-01
            • 1970-01-01
            • 2012-12-06
            • 2018-02-15
            • 2013-07-10
            相关资源
            最近更新 更多