'Access Denied' 使用应用权限访问 SharePoint Online 网站集
'Access Denied' accessing SharePoint Online Site Collection with App Permission
我使用网站集 /AppRegNew.aspx 页面在 SharePoint Online 中创建了一个 App Principal,然后使用页面 /appInv.aspx 使用以下权限授予该网站集的 App Principal FullControl XML:
<AppPermissionRequests>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
</AppPermissionRequests>
但是,当我尝试使用 CSOM 和 c#(使用 TokenHelper.cs)访问该网站集中的列表时,出现错误:
'Access denied. You do not have permission to perform this action or access this resource.'
我有另一个 App Principal,我已授予 Tenant Admin 测试权限 - 如果我使用与该 App Principal 关联的 ClientID 和 ClientSecret,我的代码运行正常(我只是阅读一些列表项进行测试)
我是不是对 AppPermissionRequests 做错了什么 XML?我还缺少其他步骤吗?我可以使用 Tenant Admin App Principal,但我想以 'right' 方式执行此操作 - 根据我的研究,它看起来应该使用 SiteCollection FullControl 权限。
我用来尝试访问的示例代码:
Uri siteUri = new Uri("https://MyCompany.sharepoint.com/sites/johntest");
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
{
Web thisWeb = context.Web;
context.Load(thisWeb);
context.ExecuteQuery();
List roomsList = context.Web.Lists.GetByTitle("Rooms");
context.Load(roomsList);
context.ExecuteQuery();
Console.WriteLine("List retrieved");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>SomeValue</Value></Eq></Where></Query></View>";
ListItemCollection listItems = roomsList.GetItems(camlQuery);
context.Load(listItems);
context.ExecuteQuery();
Console.WriteLine("Query succeeded");
}
因为您正在使用应用程序登录访问资源,通过使用 TokenHelper.GetAppOnlyAccessToken,您必须将 AllowAppOnlyPolicy="true" 属性添加到 AppPermissionRequest。
友情提醒,当AllowAppOnlyPolicy属性为true时,客户端id和客户端密码应该被视为用户名和密码,尤其是当应用程序具有高权限时,例如租户管理员或对网站集的完全控制。在任何情况下,都应保护客户端机密,但如果 AllowAppOnlyPolicy 为 false,则必须与经过身份验证的用户结合使用。
我使用网站集 /AppRegNew.aspx 页面在 SharePoint Online 中创建了一个 App Principal,然后使用页面 /appInv.aspx 使用以下权限授予该网站集的 App Principal FullControl XML:
<AppPermissionRequests>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
</AppPermissionRequests>
但是,当我尝试使用 CSOM 和 c#(使用 TokenHelper.cs)访问该网站集中的列表时,出现错误:
'Access denied. You do not have permission to perform this action or access this resource.'
我有另一个 App Principal,我已授予 Tenant Admin 测试权限 - 如果我使用与该 App Principal 关联的 ClientID 和 ClientSecret,我的代码运行正常(我只是阅读一些列表项进行测试)
我是不是对 AppPermissionRequests 做错了什么 XML?我还缺少其他步骤吗?我可以使用 Tenant Admin App Principal,但我想以 'right' 方式执行此操作 - 根据我的研究,它看起来应该使用 SiteCollection FullControl 权限。
我用来尝试访问的示例代码:
Uri siteUri = new Uri("https://MyCompany.sharepoint.com/sites/johntest");
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
{
Web thisWeb = context.Web;
context.Load(thisWeb);
context.ExecuteQuery();
List roomsList = context.Web.Lists.GetByTitle("Rooms");
context.Load(roomsList);
context.ExecuteQuery();
Console.WriteLine("List retrieved");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>SomeValue</Value></Eq></Where></Query></View>";
ListItemCollection listItems = roomsList.GetItems(camlQuery);
context.Load(listItems);
context.ExecuteQuery();
Console.WriteLine("Query succeeded");
}
因为您正在使用应用程序登录访问资源,通过使用 TokenHelper.GetAppOnlyAccessToken,您必须将 AllowAppOnlyPolicy="true" 属性添加到 AppPermissionRequest。
友情提醒,当AllowAppOnlyPolicy属性为true时,客户端id和客户端密码应该被视为用户名和密码,尤其是当应用程序具有高权限时,例如租户管理员或对网站集的完全控制。在任何情况下,都应保护客户端机密,但如果 AllowAppOnlyPolicy 为 false,则必须与经过身份验证的用户结合使用。