You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.NET Standard的CSOM连接SharePoint站点时出错求助

SharePoint CSOM (.NET Standard) 连接MFA站点的解决思路
  • 应用密码仅适配不支持现代身份验证的老旧客户端,.NET Standard版CSOM默认采用现代验证,直接用应用密码会验证失败,必须切换到OAuth身份验证方案:
    1. 在Azure AD中注册应用,为其分配SharePoint相关的API权限(如Sites.Read.All、Sites.Write.All等,根据业务需求选择)。
    2. 使用Microsoft.Identity.Client库实现交互式MFA验证,示例代码如下:
      using Microsoft.Identity.Client;
      using Microsoft.SharePoint.Client;
      using System;
      using System.Threading.Tasks;
      
      class Program
      {
          static async Task Main(string[] args)
          {
              var clientId = "你的Azure AD应用注册ID";
              var tenantId = "你的Office 365租户ID";
              var siteUrl = "目标SharePoint站点URL";
      
              var publicClientApp = PublicClientApplicationBuilder
                  .Create(clientId)
                  .WithTenantId(tenantId)
                  .Build();
      
              var scopes = new[] { $"{siteUrl}/.default" };
              var authResult = await publicClientApp.AcquireTokenInteractive(scopes).ExecuteAsync();
      
              using (var ctx = new ClientContext(siteUrl))
              {
                  ctx.ExecutingWebRequest += (sender, e) =>
                  {
                      e.WebRequestExecutor.RequestHeaders["Authorization"] = $"Bearer {authResult.AccessToken}";
                  };
      
                  var web = ctx.Web;
                  ctx.Load(web);
                  await ctx.ExecuteQueryAsync();
                  Console.WriteLine($"站点标题:{web.Title}");
              }
          }
      }
      
  • 确保安装最新版的Microsoft.SharePointOnline.CSOM和Microsoft.Identity.Client NuGet包,旧版本对现代验证支持不足。
  • 确认你的Office 365账号对目标SharePoint站点拥有足够权限(如站点编辑、管理员权限)。

内容的提问来源于stack exchange,提问作者kintela

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:00:31