2017-10-11 103 views

回答

0

添加屬性的正確方法是通過'portal.azure.com'管理門戶。

您還需要通過策略創建用戶,然後才能爲所有用戶實際使用這些屬性。

要考慮的另一件事是,擴展名在每個環境中都會有所不同,因此您需要一些自定義邏輯來解析從GraphApi獲得的用戶JSON,並檢查確定該屬性以你給這個屬性的名字。

實施例:

//Java, sorry 
private YourCustomGraphApiUser populateCustomProperty(JsonNode jsonUser) throws JsonProcessingException{ 

    YourCustomGraphApiUser azureUser = mapper.treeToValue(jsonUser, YourCustomGraphApiUser.class); 
    String[] customAttributeNames = new String()["one","two"]; //Replace this with some values from some injected properties 
    for(String attributeName : customAttributeNames){ 
     JsonNode customAttribute = jsonUser.get(attributeName); 
     if(customAttribute != null){ 
      azureUser.set(attributeName, customAttribute.asText());//Assuming custom attributes are stored in a map-like structure in YourCustomGraphApiUser. 
     } 
     else{       
      throw new NotFoundException("Error getting the name for custom attribute "+attributeName, e); 
      // OR you could ignore and just log an error. Whatever. 
     }   
    } 
    return azureUser; 
} 
+1

爲什麼我不能以編程方式添加自定義屬性?我有一個定義MyOwnUser的庫,對於那個用戶我需要自定義屬性。爲什麼我會每次爲不同的目錄在門戶中添加這些內容? – alvipeo

+0

@alvipeo這是一個很好的問題!我自己曾問了很多次。同樣令人沮喪的是,我必須手動創建具有這些屬性值的帳戶,才能使用這些帳戶。如果我在編程創建方面死心塌地,我可能會更多地關注Azure PowerShell的B2C模塊;我從來沒有這樣做,但也許你可以創建一個腳本來設置每個環境。我還沒有使用定製策略,但我理解它們是更可配置的(xml和東西),雖然它需要更多的初始努力。 – Pytry

+0

我剛剛在代碼中創建了一個創建擴展屬性的類。需要完成設置/閱讀用戶的價值 – alvipeo

1

您使用Graph API期望的應用程序對象上創建extensionProperty。

樣品JSON請求:

POST https://graph.windows.net/contoso.onmicrosoft.com/applications/269fc2f7-6420-4ea4-be90-9e1f93a87a64/extensionProperties?api-version=1.5 HTTP/1.1 
Authorization: Bearer eyJ0eXAiOiJKV1Qi...r6Xh5KVA 
Content-Type: application/json 
Host: graph.windows.net 
Content-Length: 104 
{ 
"name": "skypeId", 
"dataType": "String", 
"targetObjects": [ 
    "User" 
] 

} 

如果操作成功,它會返回一個完全合格的擴展屬性的名稱沿HTTP 201 Created狀態碼,它可以用於寫入值目標類型。 參考:azure-ad-graph-api-directory-schema-extensions