Graph API-使用.Net Core 3.1添加架构扩展 [英] Graph API - adding Schema Extension using .Net Core 3.1

查看:71
本文介绍了Graph API-使用.Net Core 3.1添加架构扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向我的Azure B2C用户添加自定义声明,所以我想到了实现此目的的方法是使用Graph API(测试版)向我的目录应用程序的用户添加架构扩展.

我为此编写了一个简单的测试

  SchemaExtension schemaExtension =新的SchemaExtension{ID = schemaName,所有者= _appClientId.ToString(),描述= string.IsNullOrWhiteSpace(schemaDesc)吗?string.Empty:schemaDesc.Trim(),TargetTypes = new List< string>{用户"},属性= new List< ExtensionSchemaProperty>{新的ExtensionSchemaProperty{名称="isGlobalAdmin",类型=布尔"},新的ExtensionSchemaProperty{名称="isOrganizationAdmin",类型=布尔"}}};SchemaExtension扩展=等待GraphClient.SchemaExtensions.要求().AddAsync(schemaExtension); 

首先,由于缺少权限,它不起作用.因此,我在目录中创建了一个新用户,并向其中添加了全局管理员"角色.然后,在应用程序身份验证设置中将 Treat应用程序作为公共客户端设置为true.固定权限问题.

但是现在我有了这个:

Microsoft.Graph.ServiceException:代码:Service_InternalServerError

我尝试更改 SchemaExtension 的参数,但无济于事.

我该如何进行这项工作?

我使用的API- Microsoft.Graph.Beta

更新-图形API初始化

 私有异步任务< GraphServiceClient>InitGraphClientWithUserAndPassword(){尝试{IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder.Create(_appClientId.ToString()).WithTenantId(_tenantId.ToString()).建造();UsernamePasswordProvider authProvider =新的UsernamePasswordProvider(publicClientApplication);//范围GraphServiceClient graphClient = new GraphServiceClient(authProvider);SecureString securePass = new NetworkCredential(",_ password).用户我=等待graphClient.Me.Request().WithUsernamePassword(_userEmail,securePass).GetAsync();返回graphClient;}抓住(前例外){const string ERR_MSG =无法创建GraphAPI客户端";_logger.LogError(ex,ERR_MSG);抛出新的IlgGraphApiException(ERR_MSG,ex);}} 

解决方案

我已经测试了您的代码.它对于Azure AD正常工作,但出现错误代码:Service_InternalServerError \ r \ n消息:遇到内部服务器错误.\ r \ n \ r \ n内部错误\ r \ n 对于Azure AD B2C.

我认为Microsoft Graph API article 说,Azure AD B2C中的自定义属性使用Microsoft.Graph.Beta

UPDATE - Graph API init

private async Task<GraphServiceClient> InitGraphClientWithUserAndPassword()
{
    try
    {
        IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create(_appClientId.ToString())
            .WithTenantId(_tenantId.ToString())
            .Build();

        UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication); // scopes

        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        SecureString securePass = new NetworkCredential("", _password).SecurePassword;
        User me = await graphClient.Me.Request()
            .WithUsernamePassword(_userEmail, securePass)
            .GetAsync();

        return graphClient;
    }
    catch (Exception ex)
    {
        const string ERR_MSG = "Could not create GraphAPI client";
        _logger.LogError(ex, ERR_MSG);
        throw new IlgGraphApiException(ERR_MSG, ex);
    }
}

解决方案

I have tested your code. It works fine for Azure AD but gets error Code: Service_InternalServerError\r\nMessage: Encountered an internal server error.\r\n\r\nInner error\r\n for Azure AD B2C.

I don't think Microsoft Graph API Create schemaExtension is supported for Azure AD B2C currently.

As this article says, Custom attributes in Azure AD B2C use Azure AD Graph API Directory Schema Extensions. Support for newer Microsoft Graph API for querying Azure AD B2C tenant is still under development.

这篇关于Graph API-使用.Net Core 3.1添加架构扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆