使用Microsoft Graph SDK如何获取用户的authentication/emailmethods值?
解决Azure AD用户信息查询中authentication/emailMethods返回null的问题
问题原因
authentication是Microsoft Graph中用户实体的导航属性,而非直接的属性字段,仅通过$select无法加载其关联数据,必须使用$expand来展开该导航属性,同时指定要获取的子属性emailMethods。
修改后的代码
将原请求配置改为同时使用Expand指定展开authentication/emailMethods:
var users = await graphServiceClient.Users.GetAsync((requestConfiguration) => { requestConfiguration.QueryParameters.Select = new string[] { "displayName", "id", "givenName", "surName", "streetAddress", "city", "state", "postalCode", "country", "userPrincipalName" }; // 展开authentication导航属性并加载emailMethods子数据 requestConfiguration.QueryParameters.Expand = new string[] { "authentication/emailMethods" }; });
额外说明
- 权限验证:确认已配置
UserAuthenticationMethod.Read.All(应用权限)或UserAuthenticationMethod.Read(委派权限),且权限已完成授予并生效。 - 性能优化:若仅需获取单个用户的邮箱验证方法,建议使用单个用户端点(
graphServiceClient.Users["{userId}"].Authentication.EmailMethods.GetAsync()),批量展开导航属性会增加请求负载,单个查询更高效。
内容的提问来源于stack exchange,提问作者wonderful world
相关产品推荐
相关产品推荐

