如果 Graph 用户被硬删除,如何获取带有更改通知的其他数据(用户主体名称)?

How to get additional data (user principle name) with change notifications if Graph user is Hard deleted?

提问人:Pearl 提问时间:10/4/2023 最后编辑:marc_sPearl 更新时间:10/4/2023 访问量:55

问:

我已经在我的 ASP.NET Core Web API 中订阅了 MS 图形用户资源的更改通知。到目前为止,当用户更新或删除(软删除)时,我都会收到通知。

订阅:

var subscriptionObj = new Subscription();
subscriptionObj.ChangeType = "updated,deleted";
subscriptionObj.NotificationUrl = $"{notificationUrl}";
subscriptionObj.Resource = "/users";
subscriptionObj.ExpirationDateTime = DateTime.Now.AddDays(5);
subscriptionObj.ClientState = $"{clientState}";

var newSubscription = await graphClient.Subscriptions.Request().AddAsync(subscriptionObj);

接收通知:

using (StreamReader reader = new StreamReader(requestBbody))
{
    string content = await reader.ReadToEndAsync();
    var notifications = JsonConvert.DeserializeObject<GraphNotifications>(content);

    foreach (var notification in notifications.Values)
    {
        var resource = notification.Resource;
        var resourceDataId = notification.ResourceData?.Id;

        if (notification.ChangeType == "updated")
        {
            try
            {
                var azureUser = await graphClient.Users[resourceDataId].Request().GetAsync();

                // DB update method call
                DbUpdate(azureUser.UserPrincipalName)
            }
            catch (Exception)
            {
                var directoryObjectSoftDeleted = await graphClient.Directory.DeletedItems[resourceDataId].Request().GetAsync();
                var azureUserSoftDeleted = (Microsoft.Graph.User)directoryObjectSoftDeleted;

                // DB update method call
                DbUpdate(azureUserSoftDeleted.UserPrincipalName)
            }
        }

        // Need to handle: if hard delete
        // else
        // {
        //     var azureUserDeleted = await graphClient.Users[resourceDataId].Request().GetAsync();
        //     DbUpdate(azureUserDeleted.UserPrincipalName)
        // }
    }

}

我通过资源数据 ID 获取 Azure 用户,并找到属于已更新/已删除资源的电子邮件地址(用户主体名称),并使用该电子邮件查询用户并更新相应用户的数据库。

目前,一切按预期工作,但我想知道如果用户被硬删除,我该如何获取资源数据 ID。即使我获得了资源数据 ID,我如何通过提供资源数据 ID(对象 ID)来查询 azure 用户,因为如果用户被硬删除,则找不到资源。我想删除我的数据库中所有硬删除和软删除的图形用户。我怎样才能做到这一点?

C# azure-active-directory microsoft-graph-api asp.net-core-webapi 更改通知

评论


答:

0赞 user2250152 10/4/2023 #1

硬删除项目没有订阅。如果用户在初始删除 30 天后被永久删除,则可以在数据库中创建一个新列或表来存储应永久删除用户的日期。

catch (Exception)
{
    var directoryObjectSoftDeleted = await graphClient.Directory.DeletedItems[resourceDataId]
                                       .Request()
                                       .Select("userPrincipalName,deletedDateTime").GetAsync();
    var azureUserSoftDeleted = (Microsoft.Graph.User)directoryObjectSoftDeleted;

    // DB update method call
    DbUpdate(azureUserSoftDeleted.UserPrincipalName, azureUserSoftDeleted.DeletedDateTime);
}

定期检查硬删除日期是否已过期,然后从数据库中删除用户。

评论

0赞 Pearl 10/4/2023
谢谢你的回答。目前,我没有在我的数据库中跟踪图形用户的对象 ID。我通过用户的电子邮件(用户主体名称)查询用户。那么有没有办法获得永久删除的用户数据,如显示名称......等通知,或者我是否必须跟踪数据库中的对象 ID?
0赞 Md Farid Uddin Kiron 10/4/2023 #2

我想知道如果用户被硬删除,我该如何获取资源数据ID。

好吧,首先,通常在 30 天后删除的用户或资源称为硬删除,这不仅是您,而且Microsoft本身也无法恢复或能够检索任何元数据或任何东西,因为它们永远删除了@user2250152指出。

https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/users-restore

但是,始终可以使用图形 API 在接下来的 30 天内提取软删除用户或资源信息。

目前,只有应用程序、servicePrincipal、组、管理单元和用户资源支持已删除邮件功能。请参考此官方文档。

var graphClient = new GraphServiceClient(requestAdapter);

var result = await graphClient.Directory.DeletedItems["{YourUserObjectID}"].GetAsync();

注意:有关更多细节,请参阅此官方文档