简体   繁体   English

Azure Active Directory Graph Client 2.0

[英]Azure Active Directory Graph Client 2.0

Anyone using the new 2.0 version of the Azure AD Graph Client? 有人使用新的2.0版Azure AD Graph Client吗?

I started fooling around with it yesterday but can't get it to work. 我昨天开始愚弄它但却无法让它发挥作用。 The GraphConnection class is marked deprecated and replaced with ActiveDirectoryClient . GraphConnection类标记为已弃用,并替换为ActiveDirectoryClient In addition all of a sudden it's all Office 365 while I just want to limit my trials to Azure Active Directory without O365. 此外,突然之间它全部是Office 365,而我只是想在没有O365的情况下将我的试用限制在Azure Active Directory中。 Documentation is hard to find, at least when you don't want to use the O365 and O365 API Tools. 至少在您不想使用O365和O365 API工具时,很难找到文档。 The AD samples on GitHub seem to be updated as well but code is still using GraphConnection class. GitHub上的AD示例似乎也在更新,但代码仍在使用GraphConnection类。 Go figure. 去搞清楚。

Not much samples/guidance on using ActiveDirectory client yet so below code using for now 关于使用ActiveDirectory客户端的样本/指导并不多,所以目前使用的代码如下

public async Task<ActionResult> Index()
        {
            List<Exception> exceptions = new List<Exception>();
            ProfileViewModel model = new ProfileViewModel();
            string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
            AuthenticationContext authContext = new AuthenticationContext(SecurityConfiguration.Authority, new NaiveSessionCache(userObjectID));
            ClientCredential credential = new ClientCredential(SecurityConfiguration.ClientId, SecurityConfiguration.AppKey);

            try
            {
                var ServiceUri = new Uri(SecurityConfiguration.GraphUrl);
                ActiveDirectoryClient client = new ActiveDirectoryClient(ServiceUri, async () =>
                {
                    var result = await authContext.AcquireTokenSilentAsync(SecurityConfiguration.GraphUrl, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                    return result.AccessToken;
                });
                try
                {

                    var users = await client.Users.ExecuteAsync();

                    var user = await client.Users[userObjectID].ExecuteAsync();


                }
                catch (Exception exc) 
                {
                    exceptions.Add(exc);
                }


            }
            catch (AdalSilentTokenAcquisitionException exc)
            {
                exceptions.Add(exc);

            }
            ViewBag.Exceptions = exceptions;
            return View(model);
        }

client.Users.ExecuteAsync() throws exceptions client.Users.ExecuteAsync()抛出异常

The response payload is a not a valid response payload. 响应有效载荷不是有效的响应有效载荷。 Please make sure that the top level element is a valid Atom or JSON element or belongs to ' http://schemas.microsoft.com/ado/2007/08/dataservices ' namespace. 请确保顶级元素是有效的Atom或JSON元素,或者属于' http://schemas.microsoft.com/ado/2007/08/dataservices '命名空间。

client.Users[userObjectID].ExecuteAsync() throws client.Users[userObjectID].ExecuteAsync()抛出

System.Reflection.TargetInvocationException with Innerexpection Expected a relative URL path without query or fragment. 带有Innerexpection的System.Reflection.TargetInvocationException预期没有查询或片段的相对URL路径。 Parameter name: entitySetName 参数名称:entitySetName

UPDATE 2/11 更新2/11

Spooky resolution: without changing one line of code client.Users.ExecuteAsync() worked as expected. 幽灵client.Users.ExecuteAsync()解决方案:无需更改一行代码client.Users.ExecuteAsync()按预期工作。 My thought is that folks at MSFT changed some stuff on the API so that response payload is now correct. 我的想法是MSFT的人改变了API上的一些东西,以便响应有效负载现在正确。 They could have mentioned that. 他们本可以提到这一点。

To get user details using v2.0 code below does the trick 使用下面的v2.0代码获取用户详细信息可以解决问题

var userFetcher = client.Users.Where(u => u.ObjectId == userObjectID);
var user = await userFetcher.ExecuteAsync();

If you are using razor to display content of the user you'll probably get razor exceptions when trying to go through collection like AssignedPlans 如果您使用剃刀显示用户的内容,那么在尝试浏览AssignedPlans集合时,您可能会遇到razor异常

The type 'System.Object' is defined in an assembly that is not referenced. “System.Object”类型在未引用的程序集中定义。 You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 您必须添加对程序集'System.Runtime,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用。

Resolution is to change compilation settings in your web.config as outlined in http://www.lyalin.com/2014/04/25/the-type-system-object-is-defined-in-an-assembly-that-is-not-reference-mvc-pcl-issue/ 解决方法是更改​​web.config中的编译设置,如http://www.lyalin.com/2014/04/25/the-type-system-object-is-defined-in-an-assembly-that-中所述。是-不参考-MVC-PCL-问题/

<compilation debug="true" targetFramework="4.5" >
      <assemblies>
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
    </compilation>

For retrieving a user entity by Id, rather than: 用于按Id检索用户实体,而不是:

var userFetcher = client.Users.Where(u => u.ObjectId == userObjectID); 
var user = await userFetcher.ExecuteAsync();

you can just use getByObjectId directly: 你可以直接使用getByObjectId:

var user = await client.Users.GetByObjectId(userObjectID).ExecuteAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Active Directory Graph Client 2.0-包含表达式 - Azure Active Directory Graph Client 2.0 - Contains Expression 使用Azure AD Graph Client获取Active Directory管理员 - Get Active Directory Administrators using Azure AD Graph Client MS Graph 和 Azure 活动目录 - MS Graph and Azure Active Directory 将 Microsoft Graph Core Python 客户端库与 Azure Active Directory 应用程序注册一起使用(客户端 ID 和密码) - Use Microsoft Graph Core Python Client Library with the Azure Active Directory App registration (client id & secret) 带有Small Business订阅的Office365 Azure Active Directory Graph客户端访问 - Office365 Azure Active Directory Graph Client access with Small Business subscription 特权不足,无法完成操作使用Azure Active Directory Graph Client API添加新用户 - Insufficient privileges to complete the operation Add new user using Azure Active Directory Graph Client API Active Directory Graph Client添加图库应用 - Active Directory Graph Client add gallery app Azure活动目录图API查询用户 - Azure active directory graph api query user Azure Active Directory Sharepoint&Graph API - Azure Active directory Sharepoint & Graph API Microsoft .net core 2.0 Azure数据湖和Azure Active Directory - Microsoft .net core 2.0 Azure Data Lake and Azure Active Directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM