c#-使用http发布请求在具有Graph API的Azure Active Directory(B2C)中创建新用户
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-使用http发布请求在具有Graph API的Azure Active Directory(B2C)中创建新用户,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3148字,纯文字阅读大概需要5分钟。
内容图文
![c#-使用http发布请求在具有Graph API的Azure Active Directory(B2C)中创建新用户](/upload/InfoBanner/zyjiaocheng/684/a375eb2ea4c242ccaf13558cac9a2a24.jpg)
我以前一直使用Active Directory身份验证库(ADAL)以编程方式添加用户,但是现在我需要定义“ signInNames”(=用户电子邮件),而ADAL似乎不可行(请告诉我即时消息是否错误) .
现在,我尝试在the documentation on MSDN之后使用HTTP POST以编程方式添加新用户(本地帐户).
//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#应用程序中执行此操作.
解决方法:
您是否授予该应用足够的权限来操作用户?对于B2C租户来说,创建用户REST API对我来说效果很好.
这是我测试的步骤:
1.通过下面的PowerShell创建应用
PowerShell:
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
New-MsolServicePrincipal -DisplayName "My New B2C Graph API App" -Type password -Value
2.将应用授予用户帐户管理员角色.
Add-MsolRoleMember -RoleObjectId fe930be7-5e62-47db-91af-98c3a49a38b1 -RoleMemberObjectId 7311370c-dac3-4f34-b2ce-b22c2a5a811e -RoleMemberType servicePrincipal
3.使用客户端凭证流获取应用程序的令牌
POST: https://login.microsoftonline.com/adb2cfei.onmicrosoft.com/oauth2/token
grant_type=client_credentials&client_id={AppPrincipalId return by PowerShell}&client_secret={client_secret}&resource=https%3A%2F%2Fgraph.windows.net
4.在下面使用REST创建用户:
POST: https://graph.windows.net/adb2cfei.onmicrosoft.com/users?api-version=1.6
authorization: bearer {token}
content-type: application/json
{
"accountEnabled": true,
"creationType": "LocalAccount",
"displayName": "Alex Wu",
"passwordProfile": {
"password": "Test1234",
"forceChangePasswordNextLogin": false
},
"signInNames": [
{
"type": "userName",
"value": "AlexW"
},
{
"type": "emailAddress",
"value": "AlexW@example.com"
}
]
}
内容总结
以上是互联网集市为您收集整理的c#-使用http发布请求在具有Graph API的Azure Active Directory(B2C)中创建新用户全部内容,希望文章能够帮你解决c#-使用http发布请求在具有Graph API的Azure Active Directory(B2C)中创建新用户所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。