簡體   English   中英

Azure Active Directory Graph Client 2.0

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM