使用全局管理员帐户访问 Office 365/SharePoint Online 被拒绝

2024-05-03

自从两天解决问题以来我都快疯了。问题是;

我正在制作一个控制台应用程序,它使用全局管理员帐户(在进行新订阅时被指定为管理员的帐户)与 SharePoint Online 进行通信。我想要实现的是,我想使用 CSOM 将自定义操作添加到 Office 365 的每个网站集和子网站。除了在注册时由 Office 365 预先创建的根网站集(即,根网站集)之外,该代码工作正常。https://xyz.sharepoint.com https://xyz.sharepoint.com)

对于根网站集的任何租户,它都会给出以下错误;

{ “SchemaVersion”:“15.0.0.0”,“LibraryVersion”:“16.0.3912.1201”,“ErrorInfo”:{ "ErrorMessage":"访问被拒绝。您无权执行 此操作或访问此 资源。”,“ErrorValue”:null,“TraceCorrelationId”:“2a47fd9c-c07b-1000-cfb7-cdffbe3ab83a”,“ErrorCode”:-2147024891,“ErrorTypeName”:“System.UnauthorizedAccessException” },"TraceCorrelationId":"2a47fd9c-c07b-1000-cfb7-cdffbe3ab83a" }

现在用户是全局管理员。我还再次将该用户添加为网站集管理员。

同一段代码在其他网站集(搜索网站集、任何新制作的网站集...)上运行良好。

这是一个代码;

        using (ClientContext spcollContext = new ClientContext(web.Url))
        {
            SecureString passWord = new SecureString();
            foreach (char c in strAdminPassword.ToCharArray()) passWord.AppendChar(c);
            SharePointOnlineCredentials creds = new SharePointOnlineCredentials(strAdminUser, passWord);
            spcollContext.Credentials = creds;
            Web currentweb = spcollContext.Web;
            spcollContext.Load(currentweb);
            spcollContext.ExecuteQuery();

       //     authCookie = creds.GetAuthenticationCookie(new Uri(web.Url));

            var existingActions2 = currentweb.UserCustomActions;
            spcollContext.Load(existingActions2);
            spcollContext.ExecuteQuery();
            var actions2 = existingActions2.ToArray();
            foreach (var action in actions2)
            {
                if (action.Description == "CustomScriptCodeForEachsite" &&
                    action.Location == "ScriptLink")
                {
                    action.DeleteObject();
                    spcollContext.ExecuteQuery();
                }
            }

            var newAction2 = existingActions2.Add();
            newAction2.Description = "CustomScriptCodeForEachsite";
            newAction2.Location = "ScriptLink";

            newAction2.ScriptBlock = scriptBlock;
            newAction2.Update();
            spcollContext.Load(currentweb, s => s.UserCustomActions);
            spcollContext.ExecuteQuery(); // GETTING ERROR ON THIS LINE. 
        }

注意:以上错误是Fiddler 的痕迹。


这种行为很可能是由, 基本上 当自定义脚本功能 is 关掉

如何验证?

您可以使用以下控制台应用程序验证站点权限:

using (var ctx = GetContext(webUri, userName, password))
{
    var rootWeb = ctx.Site.RootWeb;
    ctx.Load(rootWeb, w => w.EffectiveBasePermissions);
    ctx.ExecuteQuery();
    var permissions = rootWeb.EffectiveBasePermissions;
    foreach (var permission in Enum.GetValues(typeof(PermissionKind)).Cast<PermissionKind>())
    {
        var permissionName = Enum.GetName(typeof(PermissionKind), permission);
        var hasPermission = permissions.Has(permission);
        Console.WriteLine("Permission: {0}, HasPermission: {1}", permissionName, hasPermission);
    }   
}

where

public static ClientContext GetContext(Uri webUri, string userName, string password)
{
    var securePassword = new SecureString();
    foreach (var ch in password) securePassword.AppendChar(ch);
    return new ClientContext(webUri) {Credentials = new SharePointOnlineCredentials(userName, securePassword)};
}

When SP.PermissionKind.AddAndCustomizePages https://msdn.microsoft.com/en-us/library/office/ee556747%28v=office.14%29.aspx被设定为False,添加用户自定义操作时出现访问被拒绝错误。

Solution

根据:

对于自助创建的网站,自定义脚本被禁用 默认

Solution: enable Allow users to run custom scripts on self-service created sites

从 SharePoint 管理中心启用或禁用脚本编写

  1. 使用你的工作或学校帐户登录 Office 365。
  2. 转到 SharePoint 管理中心。
  3. 选择设置。
  4. 在自定义脚本下选择:

    • 阻止用户在个人网站上运行自定义脚本或允许 用户在个人网站上运行自定义脚本。

    • 阻止用户在用户创建的网站上运行自定义脚本或 允许用户在自助创建的网站上运行自定义脚本。

  5. 选择确定。它需要约24小时为了做出改变 影响。

由于通过 SharePoint Online 管理中心对脚本设置进行的任何更改可能需要长达24 hours要生效,您可以在特定网站集上启用脚本立即地通过 CSOM API(SharePoint Online 客户端组件 SDK http://www.microsoft.com/en-us/download/details.aspx?id=42038)如下所示:

public static void DisableDenyAddAndCustomizePages(ClientContext ctx, string siteUrl)
{
    var tenant = new Tenant(ctx);
    var siteProperties = tenant.GetSitePropertiesByUrl(siteUrl, true);
    ctx.Load(siteProperties);
    ctx.ExecuteQuery();

    siteProperties.DenyAddAndCustomizePages = DenyAddAndCustomizePagesStatus.Disabled;
    var result = siteProperties.Update();
    ctx.Load(result);
    ctx.ExecuteQuery();
    while (!result.IsComplete)
    {
        Thread.Sleep(result.PollingInterval);
        ctx.Load(result);
        ctx.ExecuteQuery();
    }
}

Usage

using (var ctx = GetContext(webUri, userName, password))
{
    using (var tenantAdminCtx = GetContext(tenantAdminUri, userName, password))
    {                  
         DisableDenyAddAndCustomizePages(tenantAdminCtx,webUri.ToString());
    }
    RegisterJQueryLibrary(ctx);
 }

where

public static void RegisterJQueryLibrary(ClientContext context)
{
    var actions = context.Site.UserCustomActions;
    var action = actions.Add();
    action.Location = "ScriptLink";
    action.ScriptSrc = "~SiteCollection/Style Library/Scripts/jQuery/jquery.min.js";
    action.Sequence = 1482;
    action.Update();
    context.ExecuteQuery();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用全局管理员帐户访问 Office 365/SharePoint Online 被拒绝 的相关文章

随机推荐