我們正在嘗試使用Microsoft Graph客戶端創建具有「ActivityFeed.Read」權限的Azure AD應用程序。下面的示例成功創建應用程序,但從此應用程序生成的令牌不包含角色「ActivityFeed.Read」。如果我們轉到Azure門戶並對新創建的應用程序進行任何簡單更改並手動保存並等待一分鐘,那麼生成的令牌需要角色。通過代碼新創建的Azure AD應用程序不會生成具有所需角色的標記
public static void AddApplication()
{
ActiveDirectoryClient activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsUser();
Application appObject = new Application { DisplayName = "MyNewTest" };
appObject.IdentifierUris.Add("https://localhost/MyNewTest/" + Guid.NewGuid());
appObject.ReplyUrls.Add("https://localhost/MyNewTest");
appObject.Homepage = "https://localhost/MyNewTest/home";
// Add Office 365 Management APIs
RequiredResourceAccess app1 = new RequiredResourceAccess();
app1.ResourceAppId = "c5393580-f805-4401-95e8-94b7a6ef2fc2";
//ActivityFeed.Read Role
app1.ResourceAccess.Add(new ResourceAccess() { Id = Guid.Parse("594c1fb6-4f81-4475-ae41-0c394909246c"), Type = "Role" });
appObject.RequiredResourceAccess.Add(app1);
PasswordCredential passWordCredential = new PasswordCredential
{
StartDate = DateTime.UtcNow,
EndDate = DateTime.UtcNow.AddYears(1),
Value = "xxxxxxxxxx"
};
appObject.PasswordCredentials.Add(passWordCredential);
activeDirectoryClient.Applications.AddApplicationAsync(appObject).Wait();
ServicePrincipal newServicePrincpal = new ServicePrincipal();
if (appObject != null)
{
newServicePrincpal.DisplayName = appObject.DisplayName;
newServicePrincpal.AccountEnabled = true;
newServicePrincpal.AppId = appObject.AppId;
activeDirectoryClient.ServicePrincipals.AddServicePrincipalAsync(newServicePrincpal).Wait();
}
}
下面是創建新應用程序後立即進行oauth2身份驗證的解碼的jwt令牌數據。
{
"aud": "https://manage.office.com",
"iss": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
"iat": 1455531167,
"nbf": 1455531167,
"exp": 1455535067,
"appid": "71da9ffb-b583-43c4-bb7a-9c6e1fe30624",
"appidacr": "1",
"idp": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
"oid": "36a47844-98e8-44d5-b69e-cf114772d1d3",
"sub": "36a47844-98e8-44d5-b69e-cf114772d1d3",
"tid": "de473ccc-dbc5-4625-8006-11e0e3ea8b7d",
"ver": "1.0"
}
以下是oauth2身份驗證的解碼jwt令牌數據,在我們做了一些手動更改並保存之後。
{
"aud": "https://manage.office.com",
"iss": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
"iat": 1455531317,
"nbf": 1455531317,
"exp": 1455535217,
"appid": "71da9ffb-b583-43c4-bb7a-9c6e1fe30624",
"appidacr": "1",
"idp": "https://sts.windows.net/de473ccc-dbc5-4625-8006-11e0e3ea8b7d/",
"oid": "36a47844-98e8-44d5-b69e-cf114772d1d3",
"roles": [
"ActivityFeed.Read"
],
"sub": "36a47844-98e8-44d5-b69e-cf114772d1d3",
"tid": "de473ccc-dbc5-4625-8006-11e0e3ea8b7d",
"ver": "1.0"
}
請讓我們知道如何以編程方式創建具有所需角色的應用程序。
昨天我們再次嘗試檢查修復程序,仍然不會生成具有角色的標記。但是,在我們進行手動編輯和保存後,使用所需角色生成的令牌。 – prabna
嗯 - 你是否控告你使用了類似我發給你的代碼片段?這應該創建應用程序角色分配,角色應顯示在令牌中。您是否嘗試回讀以查看應用程序角色分配是否實際存在(通過代碼或使用Graph Explorer)? –
我們使用相同的代碼,並且jwt令牌中的「角色」仍爲空。但手動保存後,它工作正常。我們在圖表瀏覽器中檢查了Azure AD應用程序,其中許可證爲空。此外,圖表瀏覽器的輸出與jwt令牌包含角色和沒有角色時的輸出相同。 「appRoles」:[], 「requiredResourceAccess」:[{ 「resourceAppId」: 「c5393580-f805-4401-95e8-94b7a6ef2fc2」, 「resourceAccess」:[{ 「ID」:「594c1fb6 -4f81-4475-ae41-0c394909246c「,」type「:」Role「} ] } –
prabna