我使用以下代码创建一个虚拟用户:
public void AddDummyUser(string email)
{
var user = new User
{
AccountEnabled = true,
DisplayName = "Dummy User",
UserPrincipalName = email, // email = "DummyUser@xx.onmicrosoft.com"
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextLogin = false,
Password = "Demo2016!"
},
MailNickname = "DummyUser",
GivenName = "Dummy",
Surname = "User",
JobTitle = "n.v.t.",
Department = "n.v.t.",
City = "n.v.t.",
State = "n.v.t.",
Mobile = "n.v.t.",
UsageLocation = "US"
};
var adClient = GetAdClient();
adClient.Users.AddUserAsync(user); // When I use .Wait() the application hangs and nothing happens.
}
我知道我可以连接到我的Azure AD,因为我可以通过它的电子邮件检索用户。以下代码工作得很好:
private User GetUserByEmail(string email)
{
/*
OtherMails is the "ALTERNATE EMAIL ADDRESS" in Azure AD.
Source: http://stackoverflow.com/a/35782771/40676
*/
var adClient = GetAdClient();
var userLookupTask = adClient.Users
.Where(u => u.OtherMails.Any(m => m.Equals(email, StringComparison.OrdinalIgnoreCase)))
.ExecuteAsync()
.Result
.CurrentPage
.FirstOrDefault();
User user = (User)userLookupTask;
return user;
}
两种方法属于同一类。
我在这里做错了什么?
编辑:
当我第一次调用GetUserByEmail
然后调用AddDummyUser
时,我看到正在Azure AD中创建用户。对此非常非常困惑......