![](/img/trans.png)
[英]Azure Active Directory Graph Client 2.0 - Contains Expression
[英]Azure Active Directory Graph Client 2.0
有人使用新的2.0版Azure AD Graph Client吗?
我昨天开始愚弄它但却无法让它发挥作用。 GraphConnection
类标记为已弃用,并替换为ActiveDirectoryClient
。 此外,突然之间它全部是Office 365,而我只是想在没有O365的情况下将我的试用限制在Azure Active Directory中。 至少在您不想使用O365和O365 API工具时,很难找到文档。 GitHub上的AD示例似乎也在更新,但代码仍在使用GraphConnection
类。 去搞清楚。
关于使用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()
抛出异常
响应有效载荷不是有效的响应有效载荷。 请确保顶级元素是有效的Atom或JSON元素,或者属于' http://schemas.microsoft.com/ado/2007/08/dataservices '命名空间。
client.Users[userObjectID].ExecuteAsync()
抛出
带有Innerexpection的System.Reflection.TargetInvocationException预期没有查询或片段的相对URL路径。 参数名称:entitySetName
更新2/11
幽灵client.Users.ExecuteAsync()
解决方案:无需更改一行代码client.Users.ExecuteAsync()
按预期工作。 我的想法是MSFT的人改变了API上的一些东西,以便响应有效负载现在正确。 他们本可以提到这一点。
使用下面的v2.0代码获取用户详细信息可以解决问题
var userFetcher = client.Users.Where(u => u.ObjectId == userObjectID);
var user = await userFetcher.ExecuteAsync();
如果您使用剃刀显示用户的内容,那么在尝试浏览AssignedPlans
集合时,您可能会遇到razor异常
“System.Object”类型在未引用的程序集中定义。 您必须添加对程序集'System.Runtime,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用。
解决方法是更改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>
用于按Id检索用户实体,而不是:
var userFetcher = client.Users.Where(u => u.ObjectId == userObjectID);
var user = await userFetcher.ExecuteAsync();
你可以直接使用getByObjectId:
var user = await client.Users.GetByObjectId(userObjectID).ExecuteAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.