【问题标题】:Create users in ASP.NET Core web app using Active Directory authentication使用 Active Directory 身份验证在 ASP.NET Core Web 应用程序中创建用户
【发布时间】:2017-05-02 14:48:21
【问题描述】:

如何在使用 OAuth 或 OpenID Connect 进行身份验证的 ASP.NET Core Web 应用中创建/编辑用户?

我找到的所有文档和示例都允许用户注册。 例如(active-directory-dotnet-webapp-openidconnect-aspnetcore

我的要求是能够在我们的数据库中创建/编辑用户和分配角色,然后允许这些用户使用 Azure AD 登录到 Web 应用程序。

【问题讨论】:

  • 第一:要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题对于 Stack Overflow 来说是题外话,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。 第二:由于安全问题(即没有限制或锁定登录过程,因此攻击者可以“暴力破解”密码并实施锁定可能被恶意用户用来锁定公司人员访问网络)
  • 编辑问题。

标签: c# asp.net azure asp.net-core azure-active-directory


【解决方案1】:

如果您正在构建一个可能包含 azure 广告用户管理的应用,并希望在管理员用户登录后创建/编辑用户。您可以先参考以下代码示例,了解如何使用 Azure AD 在 ASP.NET Core Web 应用程序中调用 Web API:

https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore

然后您可以使用 Azure AD 图形 api 到 create azure ad users

  1. 首先在azure门户中注册应用,设置重定向url(例如https://localhost:44371/signin-oidc),添加一个key,为你的应用配置权限,要使用azure ad graph api,你需要选择Windows Azure Active Directory,并设置委托权限Read and write directory data(需要管理员同意)。

  2. 在控制器操作(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 = "nanyuTest54@testbasic1.onmicrosoft.com"
                };
                // 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.
                //
    
            }
    

如果我误解了您的要求,请随时告诉我。

【讨论】:

  • 很好的答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 2022-08-16
  • 2022-10-04
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多