【问题标题】:Microsoft Graph SDK (C#) Group MailboxMicrosoft Graph SDK (C#) 组邮箱
【发布时间】:2019-11-19 22:12:35
【问题描述】:

我正在编写一个连接的控制台应用程序,以帮助自动执行即将进行的租户迁移。

我要做的是从当前租户中删除自定义域的所有关联,以便我可以将它们添加到新租户。

到目前为止我所拥有的:

ConsoleApp 已向 AAD 注册,并且已分配 API 权限并获得同意。

我已导入以下内容:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client

初始化一个graphClient:

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

并成功地对图表进行了.GetAsync 调用。

我需要什么帮助:

我正在尝试将群组的电子邮件更新为 'gItem.mailNickname@tenantID'。我有一个错误说“邮件字段是只读的”。通过图形资源管理器,我注意到 proxyAddresses 字段包含该组的所有别名,并且邮件被列为“SMPT:...”我试图用

覆盖 proxyAddresses 字段
var upGroup = await graphClient
    .Groups["4f629be4-f592-4520-b80d-7570f68e276e"]
    .Request()
    .GetAsync();

var updateFields = new Group
{
    ProxyAddresses = new List<string>()
    {
    "SMPT:" + upGroup.MailNickname + "@" + tenantID,
    }
};

await graphClient
    .Groups[upGroup.Id]
    .Request()
    .UpdateAsync(updateFields);

我收到权限不足的错误消息,但我已检查 AAD 以确保 Directory.ReadWrite.AllGroups.ReadWrite.All 均已配置和授予。

其他组属性可以更改,这导致我质疑我在 proxyAddresses 属性上的结构。

代码添加到列表失败

var updateFields = new Group
{
    ProxyAddresses = new string[] { "SMTP:" + upGroup.MailNickname + "@" + tenantID }
};

完整代码:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;

namespace RemoveGroupAliases
{

    class Program
    {
        private static string clientId = "";
        private static string tenantID = "";
        private static string clientSecret = "";
        private static IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithTenantId(tenantID)
            .WithClientSecret(clientSecret)
            .Build();

        static ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

        static async Task Main(string[] args)
        {
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var groups = await graphClient
                .Groups
                .Request()
                .Filter("mailEnabled+eq+true")
                .GetAsync();

            foreach (var tgroup in groups)
            {
                Console.WriteLine(tgroup.Id);
                Console.WriteLine(tgroup.DisplayName);
            };

            var upGroup = await graphClient
                .Groups["4f629be4-f592-4520-b80d-7570f68e276e"]
                .Request()
                .GetAsync();

            var updateFields = new Group
            {
                ProxyAddresses = new List<String>()
                {
                "SMTP:" + upGroup.MailNickname + "@" + tenantID
                }
            };

            await graphClient
                .Groups[upGroup.Id]
                .Request()
                .UpdateAsync(updateFields);

            Console.ReadLine();
        }
    }
}

这个想法是拉出所有启用邮件的组并删除所有域,以便我可以将其从该租户中删除并将其关联到新租户。切换域后,我将有一个不同的程序来分配域和别名。当我将鼠标悬停在 proxyAddresses 的工具提示上时,最后一行显示“只读”,这可能是我的问题,但我没有将其视为错误。

【问题讨论】:

  • Proxyaddress 只能接受一个 'SMTP' 地址,其他地址必须是 'smtp'。这可能是你的问题吗?
  • 我首先要替换整个字段值。即使使用更正的 SMTP,CreateAsync (Post) 也会收到目标不允许 HTTP 方法的错误。
  • 我已经完成并创建了一个新的应用程序注册,并在继续测试之前确保它具有管理员同意绿色复选标记。 var updateFields = new Group { ProxyAddresses = new List&lt;String&gt; { "SMTP:test.permissions@lonestarsecure.com", "smtp:test.permissions@5DShield.com" } }; await graphClient.Groups[upGroup.Id] .Request() .UpdateAsync(updateFields); 还是没有运气
  • 更新:控制台程序成功更新了描述字段,这让我相信问题在于构建 ProxyAddresses 字段的构造。有什么想法吗?
  • 你能发布一个包含 JSON 在内的整个 HTTP 请求的 pastebin 吗? (不要忘记编辑任何私钥等)

标签: c# microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

您所描述的场景根本不可能。您不能将组(或实际上任何数据)与一个租户分离并将其迁移到另一个租户。每个 AAD 租户都是一个不同的实体,并且与任何其他租户隔离。

Microsoft Graph 使用您提供的令牌将您的请求路由到生成令牌的租户内的正确 API/服务。您不能将单个令牌连接到多个租户。

此外,如documentation 中所述,属性mailproxyAddresses 是只读的。如果您希望更改群组的电子邮件别名,可以更改mailNickname 属性。这只是组的“别名”,电子邮件地址的域部分由 AAD 和 Exchange ({mailNickname}@{default.tenant.domain}) 自动分配。

【讨论】:

  • 好的,当我运行我的脚本时,我没有收到该字段是只读的错误。我能够找出 powershell 来自动化它。我只是尝试从当前租户中删除所有自定义域,并使用第二个脚本/控制台应用程序将自定义域分配给新租户中的新用户/组。感谢您的帮助。
  • 你不需要做任何这些。只需在租户中添加并验证域,它就会自动处理将添加的域分配给每个别名。将它们从当前租户中移除也是如此。唯一的限制是您不能删除原始的{name}.onmicrosoft.com 域或默认域(假设它是不同的原始域)。
  • 嘿,谢谢,我与一家公司就迁移进行了一次会议,他们告诉我转换需要大约 2 天的时间和 Powershell 才能删除域并将其添加到新的租户。我看到当您从租户中删除域时,管理员门户将取消分配域。
  • 我不知道为什么需要 2 天,但我不知道 O365 中有任何限制会导致这种情况。在将域添加到另一个租户之前,您可能需要等待删除传播,但这不会花费很长时间。
猜你喜欢
  • 2023-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
相关资源
最近更新 更多