如何在使用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