从 Azure AD 中获取用户的令牌主题标识符 (sub)
Getting User's Token Subject Identifier (sub) From Within Azure AD
我的网络应用程序正在使用多个 OAuth 2.0 身份提供者,并希望从访问令牌响应的 id_token 中检索“sub”并匹配它一个存储在我的应用程序的数据库中,因为'sub'是用户所在的任何系统的唯一ID,并且它是id_token.[=10中的一个立场字段=]
我的问题是:
是否有 obvious/convenient 方法从 Azure AD 门户中检索用户的 Token Subject Identifier(又名 sub)?我知道“对象 ID”(又名 对象标识符 或 oid)是用户配置文件的一部分Azure AD 门户。但是,'oid' 不是 JWT id_token 中的标准字段(例如,Azure AD 使用它,但 Google Identity 不使用),但是 'sub' 是。
在 Azure 管理门户中,您只能看到 Active Directory 中用户的对象 ID。
但是在 C# 代码中,如果您拥有该用户的 JWT 令牌,您可以像下面这样对其进行解码并从中获取您想要的任何内容 属性:
var token = new JwtSecurityToken(jwtToken);
var oid = token.Claims.FirstOrDefault(m=>m.Type == "oid").Value;
var sub = token.Claims.FirstOrDefault(m => m.Type == "sub").Value;
但是,如果您没有用户的用户名密码,则无法从 AAD 为他们获取 JWT 令牌。
或者,您可以使用 AAD Graph API 从 AAD 获取更详细的用户信息,但即使是 Azure Graph API 也不会在响应中包含 "SUB",只有对象 ID:
https://msdn.microsoft.com/en-us/library/azure/dn151678.aspx
这里是使用 AAD Graph 的 GET 用户调用的响应:
{
"odata.metadata": "https://graph.windows.net/contoso.onmicrosoft.com/$metadata#directoryObjects/Microsoft.WindowsAzure.ActiveDirectory.User/@Element",
"odata.type": "Microsoft.WindowsAzure.ActiveDirectory.User",
"objectType": "User",
"objectId": "4e971521-101a-4311-94f4-0917d7218b4e",
"accountEnabled": true,
"assignedLicenses": [],
"assignedPlans": [],
"city": null,
"country": null,
"department": null,
"dirSyncEnabled": null,
"displayName": "Alex Wu",
"facsimileTelephoneNumber": null,
"givenName": null,
"jobTitle": null,
"lastDirSyncTime": null,
"mail": null,
"mailNickname": "AlexW",
"mobile": null,
"otherMails": [],
"passwordPolicies": null,
"passwordProfile": null,
"physicalDeliveryOfficeName": null,
"postalCode": null,
"preferredLanguage": null,
"provisionedPlans": [],
"provisioningErrors": [],
"proxyAddresses": [],
"state": null,
"streetAddress": null,
"surname": null,
"telephoneNumber": null,
"usageLocation": null,
"userPrincipalName": "Alex@contoso.onmicrosoft.com"
}
我的网络应用程序正在使用多个 OAuth 2.0 身份提供者,并希望从访问令牌响应的 id_token 中检索“sub”并匹配它一个存储在我的应用程序的数据库中,因为'sub'是用户所在的任何系统的唯一ID,并且它是id_token.[=10中的一个立场字段=]
我的问题是: 是否有 obvious/convenient 方法从 Azure AD 门户中检索用户的 Token Subject Identifier(又名 sub)?我知道“对象 ID”(又名 对象标识符 或 oid)是用户配置文件的一部分Azure AD 门户。但是,'oid' 不是 JWT id_token 中的标准字段(例如,Azure AD 使用它,但 Google Identity 不使用),但是 'sub' 是。
在 Azure 管理门户中,您只能看到 Active Directory 中用户的对象 ID。
但是在 C# 代码中,如果您拥有该用户的 JWT 令牌,您可以像下面这样对其进行解码并从中获取您想要的任何内容 属性:
var token = new JwtSecurityToken(jwtToken);
var oid = token.Claims.FirstOrDefault(m=>m.Type == "oid").Value;
var sub = token.Claims.FirstOrDefault(m => m.Type == "sub").Value;
但是,如果您没有用户的用户名密码,则无法从 AAD 为他们获取 JWT 令牌。
或者,您可以使用 AAD Graph API 从 AAD 获取更详细的用户信息,但即使是 Azure Graph API 也不会在响应中包含 "SUB",只有对象 ID:
https://msdn.microsoft.com/en-us/library/azure/dn151678.aspx
这里是使用 AAD Graph 的 GET 用户调用的响应:
{
"odata.metadata": "https://graph.windows.net/contoso.onmicrosoft.com/$metadata#directoryObjects/Microsoft.WindowsAzure.ActiveDirectory.User/@Element",
"odata.type": "Microsoft.WindowsAzure.ActiveDirectory.User",
"objectType": "User",
"objectId": "4e971521-101a-4311-94f4-0917d7218b4e",
"accountEnabled": true,
"assignedLicenses": [],
"assignedPlans": [],
"city": null,
"country": null,
"department": null,
"dirSyncEnabled": null,
"displayName": "Alex Wu",
"facsimileTelephoneNumber": null,
"givenName": null,
"jobTitle": null,
"lastDirSyncTime": null,
"mail": null,
"mailNickname": "AlexW",
"mobile": null,
"otherMails": [],
"passwordPolicies": null,
"passwordProfile": null,
"physicalDeliveryOfficeName": null,
"postalCode": null,
"preferredLanguage": null,
"provisionedPlans": [],
"provisioningErrors": [],
"proxyAddresses": [],
"state": null,
"streetAddress": null,
"surname": null,
"telephoneNumber": null,
"usageLocation": null,
"userPrincipalName": "Alex@contoso.onmicrosoft.com"
}