簡體   English   中英

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

[英]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#.net 應用程序,它將使用客戶端 object model 方法與 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); } }

謝謝大家,誰提供了解決方案。 下面是工作代碼:

 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)); } }

如果您可以提供用戶名、密碼和域,則此代碼對我有用:

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

從您提供的代碼片段來看,您似乎正在嘗試連接到SharePoint Online (O365)。 您在代碼中使用的網絡憑據 object 用於連接到傳統的“本地”SharePoint 安裝。 您應該改用SharePointOnlineCredentials

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

如果您想在客戶端訪問 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); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM