如何在使用OAuth或OpenID Connect连接身份验证的ASP.NET Core Web应用程序中创建/编辑用户?使用Active Directory验证在ASP.NET Core web应用程序中创建用户
我找到的所有文档和示例都允许用户注册。 例如(active-directory-dotnet-webapp-openidconnect-aspnetcore)
我的要求是能够创建/编辑用户并在我们的数据库中分配角色,然后允许这些用户使用Azure AD登录到Web应用程序。
如何在使用OAuth或OpenID Connect连接身份验证的ASP.NET Core Web应用程序中创建/编辑用户?使用Active Directory验证在ASP.NET Core web应用程序中创建用户
我找到的所有文档和示例都允许用户注册。 例如(active-directory-dotnet-webapp-openidconnect-aspnetcore)
我的要求是能够创建/编辑用户并在我们的数据库中分配角色,然后允许这些用户使用Azure AD登录到Web应用程序。
如果您正在构建可能包含天蓝色广告用户管理的应用程序,并希望在管理员用户登录后创建/编辑用户。你可以先参考下面的代码示例如何使用调用Web API在ASP.NET核心的Web应用程序Azure的AD:
https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore
那么你可以使用Azure的AD图形API来create azure ad users:
首先登记在蔚蓝的门户应用程序,设置重定向URL(https://localhost:44371/signin-oidc例如),加一键,配置权限的应用程序,要使用蔚蓝广告图形API,你需要选择Windows Azure Active Directory
,并设置委托权限Read and write directory data
(需要管理员同意) 。
在控制器动作(HttpPost),你可以使用下面的代码来创建一个用户:
AuthenticationResult result = null;
try
{
string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
ClientCredential credential = new ClientCredential(Startup.ClientId, Startup.ClientSecret);
result = await authContext.AcquireTokenSilentAsync("https://graph.windows.net", credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
var userData = new
{
accountEnabled = true,
displayName = "nan yu",
mailNickname = "nanyu",
passwordProfile = new
{
password = "xxxxxx",
forceChangePasswordNextLogin = false
},
userPrincipalName = "[email protected]"
};
// Forms encode todo item, to POST to the Azure AD graph api.
HttpContent content = new StringContent(JsonConvert.SerializeObject(userData), System.Text.Encoding.UTF8, "application/json");
//
// Add the azure ad user.
//
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://graph.windows.net/myorganization/users?api-version=1.6");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);
//
// Return user in the view.
//
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
else
{
//
// If the call failed with access denied, then drop the current access token from the cache,
// and show the user an error indicating they might need to sign-in again.
//
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
}
}
}
catch (Exception ee)
{
//
// The user needs to re-authorize. Show them a message to that effect.
//
}
如果我误解你的要求,请随时让我知道。
很棒的回答。谢谢! – Newport99
第一:_Questions要求我们推荐或找到书籍,工具,软件库,教程或其他非本地资源,因为它们倾向于吸引舆论的答案和垃圾邮件,因此无法用于Stack Overflow。相反,请描述问题以及到目前为止解决该问题所做的工作。第二:由于安全问题,AD不支持在ASP.NET Core中开箱即用(即没有对登录过程进行限制或锁定攻击者可能“暴力破解”密码并实施锁定可能被恶意用户用来锁定公司人员访问网络) – Tseng
编辑问题。 – Newport99