【发布时间】:2016-09-11 19:24:54
【问题描述】:
我之前一直使用 Active Directory 身份验证库 (ADAL) 以编程方式添加用户,但现在我需要定义“signInNames”(= 用户电子邮件),而这似乎不适用于 ADAL(请告诉我是否我错了)。
现在我正在尝试使用 HTTP POST 以编程方式添加一个新用户(本地帐户),遵循the documentation on MSDN。
//Get access token (using ADAL)
var authenticationContext = new AuthenticationContext(AuthString, false);
var clientCred = new ClientCredential(ClientId, ClientSecret);
var authenticationResult = authenticationContext.AcquireTokenAsync(ResourceUrl, clientCred);
var token = authenticationResult.Result.AccessToken;
//HTTP POST CODE
const string mail = "new@email.com";
// Create a new user object.
var user = new CustomUser
{
accountEnabled = true,
country = "MS",
creationType = "LocalAccount",
displayName = mail,
passwordPolicies = "DisablePasswordExpiration,DisableStrongPassword",
passwordProfile = new passwordProfile { password = "jVPmEm)6Bh", forceChangePasswordNextLogin = true },
signInNames = new signInNames { type = "emailAddress", value = mail }
};
var url = "https://graph.windows.net/" + TenantId + "/users?api-version=1.6";
var jsonObject = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = client.PostAsync(url,
new StringContent(JsonConvert.SerializeObject(user).ToString(),
Encoding.UTF8, "application/json"))
.Result;
if (response.IsSuccessStatusCode)
{
dynamic content = JsonConvert.DeserializeObject(
response.Content.ReadAsStringAsync()
.Result);
// Access variables from the returned JSON object
var appHref = content.links.applications.href;
}
}
但我没有成功,得到这个回复:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:....}
任何想法我应该做什么?我成功使用了 Powershell 脚本,但我需要在我的 C# 应用程序中执行此操作。
【问题讨论】:
标签: c# adal azure-ad-graph-api