【问题标题】:Upload File to SharePoint Online Library via Web Application通过 Web 应用程序将文件上传到 SharePoint Online 库
【发布时间】:2018-03-28 18:06:12
【问题描述】:

我正在尝试创建一个允许用户将文件上传到 SharePoint Online 站点的 Web 应用程序(使用 C# 的 ASP.NET MVC)。

此代码在 SharePoint Online 中有效仅当我使用我的实际电子邮件和密码。

    using (ClientContext context = new ClientContext(url))
    {

        var password = new SecureString();

        foreach(var c in "password")
        {
            password.AppendChar(c);
        }

        context.Credentials = new SharePointOnlineCredentials(email, password);

        //code to upload file
    }

但我不想将我的凭据用于此 Web 应用程序的所有用户。因此,我一直在对此进行一些研究,并发现了基于声明的身份验证。 https://code.msdn.microsoft.com/Remote-Authentication-in-b7b6f43c#content

我肯定会尝试这个,但我仍然想知道这是否是实现我想做的唯一方法。 Web 应用程序使用 Windows 身份验证对用户进行身份验证,sharepoint 在线站点也是如此。 Sharepoint 在线站点无法从 Web 应用程序获取凭据,这真的是真的吗?

【问题讨论】:

  • 如果您有一个服务帐户,将凭据存储在 Web 配置中,并在共享点库中为上传它的人提供了一个列,我们必须对其复杂性进行相同的操作以使用身份验证,如果你错过了将用户添加到网站这也是一个问题,只是我的想法
  • @GuruparanGiritharan 谢谢。我完全同意。我们决定为此使用服务帐户

标签: c# asp.net-mvc sharepoint-2013 sharepoint-online


【解决方案1】:

我们可以通过 REST API 获取访问令牌来上传文件,这里有一篇文章供您参考: SharePoint Online remote authentication (and Doc upload)

关于如何通过 CSOM 将文件上传到文档,这里有一个演示供您参考:

    /// <summary>
    /// upload file to document library
    /// </summary>
    /// <param name="siteUrl">http://sp/sites/DevSite</param>
    /// <param name="filePath">C:\\</param>
    /// <param name="fileName">hello2.txt</param>
    /// <param name="docLibName">MyDocLib</param>
    public static void uploadFile(string siteUrl, string filePath, string fileName, string docLibName)
    {
        siteUrl = siteUrl.EndsWith("/") ? siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;           
        ClientContext context = new ClientContext(siteUrl);
        List docLib = context.Web.Lists.GetByTitle(docLibName);
        context.Load(docLib);
        context.ExecuteQuery();

        Byte[] bytes = System.IO.File.ReadAllBytes(filePath + fileName);

        FileCreationInformation createFile = new FileCreationInformation();

        createFile.Content = bytes;
        createFile.Url = siteUrl + "/" + docLibName + "/" + fileName;
        createFile.Overwrite = true;
        Microsoft.SharePoint.Client.File newFile = docLib.RootFolder.Files.Add(createFile);
        newFile.ListItemAllFields.Update();
        context.ExecuteQuery();
    }

【讨论】:

  • 请问,这里的认证部分在哪里?
  • 为 Office 365 做这件事怎么样
猜你喜欢
  • 1970-01-01
  • 2019-07-06
  • 2010-09-21
  • 1970-01-01
  • 2014-07-01
  • 2010-10-01
  • 1970-01-01
  • 2013-09-21
  • 2018-12-28
相关资源
最近更新 更多