![](/img/trans.png)
[英]How to get User Id and Access Token in Microsoft Graph API C#
[英]How to get user id from email address in Microsoft Graph API C#
我想將用戶添加到組,但我沒有用戶的id
,我只有 email 地址。
這是代碼:
User userToAdd = await graphClient
.Users["objectID"]
.Request()
.GetAsync();
await graphClient
.Groups["groupObjectID"]
.Members
.References
.Request()
.AddAsync(userToAdd);
有人可以幫助我如何使用 Microsoft Graph 從 email 地址檢索 ObjectId(用戶 ID)嗎?
您可以通過幾種不同的方式查找用戶。
從/users
端點,您可以使用他們的id
(分配給每個帳戶的 GUID)或他們的userPrincipalName
(他們的默認域的電子郵件別名):
// Retrieve a user by id
var user = await graphClient
.Users["00000000-0000-0000-0000-000000000000"]
.Request()
.GetAsync();
// Retrieve a user by userPrincipalName
var user = await graphClient
.Users["user@tenant.onmicrosoft.com"]
.Request()
.GetAsync();
如果您使用授權代碼或隱式OAuth 授權,您還可以查找通過/me
端點進行身份驗證的用戶:
var user = await graphClient
.Me
.Request()
.GetAsync();
除了上面的這些正確答案。
userPrincipalName
或id
不是外部電子郵件地址。 這就是我手動完成注冊時的情況。 您可以使用此過濾器:
var user = await _graphClient.Users
.Request()
.Filter($"identities/any(c:c/issuerAssignedId eq '{email}' and c/issuer eq 'contoso.onmicrosoft.com')")
.GetAsync();
源代碼: https : //docs.microsoft.com/de-de/graph/api/user-list? view = graph-rest-1.0 & tabs =csharp
使用此配置創建用戶:
Microsoft.Graph.User graphUser = new Microsoft.Graph.User
{
AccountEnabled = true,
PasswordPolicies = "DisablePasswordExpiration,DisableStrongPassword",
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = false,
Password = accountModel.Password,
},
Identities = new List<ObjectIdentity>
{
new ObjectIdentity()
{
SignInType = "emailAddress",
Issuer ="contoso.onmicrosoft.com",
IssuerAssignedId = email
}
},
};
您可以使用id
或userPrincipalName
(這是一個電子郵件地址)從 Graph API 檢索用戶的詳細信息。
GET /users/{id | userPrincipalName}
您是否嘗試使用電子郵件地址作為objectID
?
如果您需要檢索訪客用戶可以使用:
public static async Task<User> GetGuestUserByEmail(string email)
{
try
{
var request = await graphClient
.Users
.Request()
.Filter($"userType eq 'guest' and mail eq '{email}'") // apply filter
.GetAsync();
var guestUsers = request.CurrentPage.ToList();
var user = guestUsers.FirstOrDefault();
return user;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error: {ex.Message}");
return null;
}
}
實際上沒有真正可靠的方法來做到這一點。 AAD 中的電子郵件地址不是唯一的。 UPN 通常是一個電子郵件地址,它是獨一無二的,但有一個令人討厭的問題:如果該人恰好有一個長電子郵件地址並且他們是訪客用戶,那么 UPN 似乎是被截斷為 54 個字符的電子郵件,然后已添加#EXT#,甚至可以在刪除#EXT# 后進行更改,其中@ 符號前最多可以有64 個字符,並且不必是類似於他們實際電子郵件地址的任何內容。
您應該能夠真正找到它們的唯一方法是使用它們的用戶對象的 GUID。
https:\/\/docs.microsoft.com\/en-us\/answers\/questions\/81053\/user-principal-name-and-ext.html<\/a>
有一種使用 Open ID 機制獲取電子郵件地址的可靠方法。 使用openid
作為 scope,您可以從https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration獲取userinfo_endpoint
端點
GET https://graph.microsoft.com/oidc/userinfo (this endpoint shouldn't be hard coded)
{
"sub": "uniqueid",
"name": "Name",
"family_name": "Name",
"given_name": "Name",
"picture": "https://graph.microsoft.com/v1.0/me/photo/$value",
"email": "email@example.com"
}
https://docs.microsoft.com/en-us/azure/active-directory/develop/userinfo
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.