通过 SharePoint 仅限应用令牌授予访问权限

SharePoint 仅限应用是设置应用主体的旧模型,但仍非常相关。 此模型适用于 SharePoint Online 和本地 SharePoint (2013/2016/2019/subscription edition) ,非常适合准备应用程序以便从本地 SharePoint 迁移到 SharePoint Online。 以下步骤演示如何设置具有租户完全控制权限的应用主体,但也可以使用此方法仅授予读取权限。

重要

自 2023 年 11 月 27 日起,已停用 Azure ACS (访问控制 Services for SharePoint Online) ,请查看完整的停用公告以了解详细信息。 在 SharePoint 上下文之外使用 Azure ACS 已于 2018 年 11 月 7 日停用,现已停用。

停用意味着该功能不会获得任何新投资,但仍受支持。 生命周期结束意味着该功能将停用,不再可供使用。

对于新租户,默认禁用使用仅 ACS 应用访问令牌的应用。 建议使用新式且更安全的仅限 Azure AD 应用的模型。 但是,可以通过运行“set-spotenant -DisableCustomAppAuthentication $false”来更改行为, (需要最新的 SharePoint 管理员 PowerShell) 。

设置具有租户权限的仅限应用主体

注意

默认情况下,网站集管理员无法在 AppRegNew.aspx 中的 Azure ACS 中注册外接程序,除非 SharePoint 租户管理员明确允许。有关详细信息,请参阅 Set-SPOTenant

导航到租户 ((例如 https://contoso.sharepoint.com) )中的网站,然后调用 appregnew.aspx 页面 (例如 https://contoso.sharepoint.com/_layouts/15/appregnew.aspx) 。 在此页上,单击“生成”按钮,生成客户端 ID 和客户端密码,并填写剩余的信息,如下面的屏幕截图所示。

& 机密创建新的客户端 ID

重要

请存储检索到的信息(客户端 ID 和客户端密码),因为需要在下一步中用到此类信息!

下一步是向新建的主体授予权限。 因为要授予的是租户范围内的权限,所以只能通过租户管理网站上的 appinv.aspx 页面授予此权限。 可以通过 https://contoso-admin.sharepoint.com/_layouts/15/appinv.aspx 来访问此网站。 在页面加载后,添加客户端 ID,并查找已创建的主体:

向新主体授予权限

必须提供描述所需权限的权限 XML,才能授予权限。 由于此应用需要能够访问所有网站,并且还需要结合使用仅限应用访问和搜索功能,因此需要下列权限:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
</AppPermissionRequests>

单击“创建”后,将会看到权限许可对话框。 按“信任它”,以授予权限:

使用 appregnew.aspx

重要

请保护所创建的客户端 ID/密码组合,就像保护管理员帐户一样。 使用此客户端 ID/密码,可以在 SharePoint Online 环境中读取/更新所有数据!

完成准备工作后,接下来下一章节将继续介绍如何通过客户端 ID 和密码组合使用所创建的应用主体。

通过 PnP PowerShell 使用此主体

如果要将生成的仅限应用注册与 PnP PowerShell 结合使用,可以通过使用以下工具连接到本地 SharePoint 或 SharePoint Online 环境来实现此操作:

Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/demo -ClientId [Your Client ID] -ClientSecret "[Your Client Secret]"

注意

PnP PowerShell 是一种开放源代码解决方案,其中包含为其提供支持的活动社区。 没有用于 Microsoft 开放源代码工具支持的 SLA。

使用 PnP 框架库在应用程序中使用此主体

在第一步中,添加 PnP 框架库 nuget 包: https://www.nuget.org/packages/PnP.Framework。 完成此操作后,可以使用以下代码构造:

string siteUrl = "https://contoso.sharepoint.com/sites/demo";
using (var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))
{
    cc.Load(cc.Web, p => p.Title);
    cc.ExecuteQuery();
    Console.WriteLine(cc.Web.Title);
};

在应用程序中使用此主体而不使用 PnP 框架库

创建并许可主体后,便可以使用此主体的 ID 和密码来请求获取访问权限。 TokenHelper.cs 类将使用应用程序的配置文件中的 ID 和机密。

using Microsoft.SharePoint.Client;
using System;

namespace AzureACSAuth
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://contoso.sharepoint.com/sites/demo";

            //Get the realm for the URL
            string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(siteUrl));

            //Get the access token for the URL.  
            string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(siteUrl).Authority, realm).AccessToken;

            //Create a client context object based on the retrieved access token
            using (ClientContext cc = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken))
            {
                cc.Load(cc.Web, p => p.Title);
                cc.ExecuteQuery();
                Console.WriteLine(cc.Web.Title);
            }
        }
    }
}

示例 app.config 如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- Use AppRegNew.aspx and AppInv.aspx to register client id with secret -->
    <add key="ClientId" value="[Your Client ID]" />
    <add key="ClientSecret" value="[Your Client Secret]" />
  </appSettings>
</configuration>

注意

可以将 AppForSharePointOnlineWebToolkit NuGet 包添加到解决方案中,从而在项目中轻松插入 TokenHelper.cs 类。