首页 > 解决方案 > Azure AD Graph API - 将用户添加到应用程序获取 PlatformNotSupportedException

问题描述

我的目标是将用户添加到 Azure 中的应用程序。

我仅将旧的 Azure AD Graph API 用于此方法,因为较新的 Microsoft Graph API 目前不支持此功能。

通过消除过程,我发现错误发生在开始时,尝试通过 id 获取用户时。

我得到的错误是;

System.InvalidOperationException: An error occurred while processing this request. ---> System.PlatformNotSupportedException: Secure binary serialization is not supported on this platform.

我的方法代码;

public async Task AddUserToService(string userId)
{
    try
    {
        var user = await activeDirectoryClient.Users.GetByObjectId(userId).ExecuteAsync() as User;

        var appRoleAssignment = new AppRoleAssignment()
        {
            ResourceId = Guid.Parse(applicationId),
            PrincipalId = Guid.Parse(userId),
            Id = Guid.Parse(roleId)
        };

        user.AppRoleAssignments.Add(appRoleAssignment);
        await user.UpdateAsync();
    } catch (Exception e)
    {
        _logger.Log(Microsoft.Extensions.Logging.LogLevel.Error, "Error occurred during retrieval; " + e);
    }
}

标签: c#azure-active-directoryazure-ad-graph-api

解决方案


我对旧图使用直接休息 httpClient 调用。

我只是将其发布为参考 - 请注意 url (1.6) 上的显式版本。我还发布了我反序列化的对象,这可能与官方对象模式不匹配。

// OLD Graph End point    //  like ... https://graph.windows.net/{tenant-id}/users/{id}/appRoleAssignments?api-version=1.6
   urlUserInviteToUse = "https://graph.windows.net/" + m_CfgHlp.TenIdInB2C + "/" + ObjFamilyName + "/" + DirObjIdToGet + "/" + ObjFunctionCall + "?api-version=1.6";

由于其余 api 字符串有效负载,我有效地使用 JsonConvert.DeserializeObject 从有效负载转到对象类。请注意,日期没有被反序列化为日期。

public class AppRoleAssignmentsRoot
{
    public string odatametadata { get; set; }
    public AppRoleAssignment[] value { get; set; }
}

public class AppRoleAssignment
{
    public string odatatype { get; set; }
    public string objectType { get; set; }
    public string objectId { get; set; }
    public object deletionTimestamp { get; set; }
    public object creationTimestamp { get; set; }
    public string id { get; set; }
    public string principalDisplayName { get; set; }
    public string principalId { get; set; }
    public string principalType { get; set; }
    public string resourceDisplayName { get; set; }
    public string resourceId { get; set; }
}

希望能帮助到你。


推荐阅读