控制Office 365组的外部访问

时间:2019-05-07 10:06:43

标签: azure-active-directory microsoft-graph microsoft-graph-sdks

是否可以从c#启用/禁用外部访问pr 365组。我可以看到某些PowerShell cmd具有一个名为AllowGuestsUsers的属性,但是我在Microsoft Graph或类似物中找不到任何内容?

1 个答案:

答案 0 :(得分:0)

这些设置通过Microsoft Graph group settings进行管理。可以在整个租户范围内设置设置(影响所有组),并且可以为特定组设置设置(仅影响该组)。

下面是使用Microsoft Graph SDK for .NET的几个示例,以说明如何更改这些设置:

禁止来宾用户访问Office 365组

下面的示例更新Office 365组中来宾用户的承租人范围的设置,以禁用添加来宾用户,并禁用现有来宾用户对组内容的访问。大致相当于Use PowerShell to control guest access中描述的步骤。

var groupSettingsName = "Group.Unified";

// Get the group settings
var groupSettingsResult = await graph.GroupSettings.Request().GetAsync();
var groupSettings = groupSettingsResult
    .FirstOrDefault(s => s.DisplayName == groupSettingsName);

// If these settings don't exist, add them (with their default values)
if (groupSettings == null)
{
    // Get the settings template
    var groupSettingsTemplates = await graph.GroupSettingTemplates.Request().GetAsync();
    var unifiedGroupSettingTemplate = groupSettingsTemplates
        .First(g => g.DisplayName == groupSettingsName);

    // Create a new setting based on the template
    var newGroupSettings = new GroupSetting()
    {
        TemplateId = unifiedGroupSettingTemplate.Id,
        Values = unifiedGroupSettingTemplate.Values.Select(
            v => new SettingValue()
            {
                Name = v.Name,
                Value = v.DefaultValue
            })
    };

    // Create the settings
    groupSettings = await graph.GroupSettings.Request().AddAsync(newGroupSettings);
}

// Update settings (if needed)
var settings = groupSettings.Values.ToDictionary(x => x.Name, x => x);
if (settings["AllowToAddGuests"].Value.ToLower() != "false"
    || settings["AllowGuestsToAccessGroups"].Value.ToLower() != "false")
{
    settings["AllowGuestsToAccessGroups"].Value = "false";
    settings["AllowToAddGuests"].Value = "false";
    await graph.GroupSettings[groupSettings.Id].Request()
        .UpdateAsync(new GroupSetting() { Values = settings.Values });
}

禁止将来宾用户添加到特定的Office 365组

在下面的示例中,我们要在特定的组上进行设置,以禁止将其他来宾用户添加到该组中。

var groupGuestSettingsName = "Group.Unified.Guest";

// Get the group in question
var groupResult = await graph.Groups.Request()
    .Filter("displayName eq 'Test_Office365_group'").GetAsync();
var group = groupResult.First();

// Get the group's settings relating to guests
var groupSettingsResult = await graph.Groups[group.Id].Settings.Request().GetAsync();        
var groupSettings = groupSettingsResult
    .FirstOrDefault(s => s.DisplayName == groupGuestSettingsName);

// If these settings don't exist on the group, add them (with their default values)
if (groupSettings == null)
{
    // Get the settings template
    var groupSettingsTemplates = await graph.GroupSettingTemplates.Request().GetAsync();
    var unifiedGroupGuestSettingTemplate = groupSettingsTemplates
        .First(g => g.DisplayName == groupGuestSettingsName);

    // Create a new group setting based on the template
    var newGroupSettings = new GroupSetting()
    {
        TemplateId = unifiedGroupGuestSettingTemplate.Id,
        Values = unifiedGroupGuestSettingTemplate.Values.Select(
            v => new SettingValue()
            {
                Name = v.Name,
                Value = v.DefaultValue
            })
    };

    // Add these settings to the group
    groupSettings = await graph.Groups[group.Id].Settings.Request().AddAsync(newGroupSettings);
}

// Change AllowToAddGuests setting to false, if needed
var settings = groupSettings.Values.ToDictionary(x => x.Name, x => x);
if (settings["AllowToAddGuests"].Value.ToLower() != "false")
{
    settings["AllowToAddGuests"].Value = "False";
    await graph.GroupSettings[groupSettings.Id].Request()
        .UpdateAsync(new GroupSetting() { Values = settings.Values });
}