C# 使用Active directory表单ASP.NET MVC 5标识

C# 使用Active directory表单ASP.NET MVC 5标识,c#,authentication,asp.net-mvc-5,active-directory,asp.net-identity,C#,Authentication,Asp.net Mvc 5,Active Directory,Asp.net Identity,我有一个应用程序,我想在MVC应用程序中使用active directory,通过身份验证进行个人身份验证。 我的想法是,每个用户都可以使用带有广告凭据的表单登录。 并使用标识管理角色和权限。 我正在尝试使用Owin,现在我可以使用AD进行身份验证。但我无法将角色或声明分配给用户,因为我的数据库中没有这些角色或声明。 如何将身份验证与AD以及个人身份验证中的角色和声明结合起来? 我用这首短裙做广告认证。 了解这篇文章的由来已久;但是,我想和大家分享我做了些什么来让它发挥作用。 基本思想是使用AD

我有一个应用程序,我想在MVC应用程序中使用active directory,通过身份验证进行个人身份验证。 我的想法是,每个用户都可以使用带有广告凭据的表单登录。 并使用标识管理角色和权限。 我正在尝试使用Owin,现在我可以使用AD进行身份验证。但我无法将角色或声明分配给用户,因为我的数据库中没有这些角色或声明。 如何将身份验证与AD以及个人身份验证中的角色和声明结合起来? 我用这首短裙做广告认证。

了解这篇文章的由来已久;但是,我想和大家分享我做了些什么来让它发挥作用。 基本思想是使用AD进行身份验证。然后在Identity中复制凭据,并将现有的身份角色应用于新登录。 但请理解,如果系统要求更改AD密码,这将导致问题。广告的密码不会用此代码更新身份

其他要考虑的事情是使用AD的组和角色而不是身份。 我从一个实现OWIN和Identity 2.0的应用程序开始。然后,我按照您上面引用的教程添加了广告身份验证。因为我从Identity和OWIN开始,所以所有的登录代码都已准备就绪

  • 我引用了System.DirectoryServices.AccountManagement
  • 创建了他的AdAuthenticationService类
  • AccountController中存在我的LoginController代码。我在验证模型之后和身份验证代码之前,将以下代码放在公共异步任务登录(LoginViewModel model,string returnUrl)中。请注意,我在他的代码块中删除了return。我希望它继续验证的身份,如果广告认证工作。如果没有,它将在返回登录视图时列出所有“错误”

        var authenticationManager = HttpContext.GetOwinContext().Authentication;
        var authService = new AdAuthenticationService(authenticationManager);
        var authenticationResult = authService.SignIn(model.UserName, model.Password);
    
        if (authenticationResult.IsSuccess)
        {
            ModelState.AddModelError("", "AD Authenticated: " + model.UserName);
            // we are in
            // check to see if user exixts in AspNetUsers.  If not, add with default role.
    
            var inAsp = UserManager.Users.Any(x => x.UserName == model.UserName);
            if (!inAsp)
            {
                ModelState.AddModelError("", "Did not find User: " + model.UserName);
                var newUser = new ApplicationUser
                {
                    UserName = model.UserName,
                    Email = model.UserName + "@a_company.com"
                };
                ModelState.AddModelError("", "Set new user " + model.UserName);
                // Add the AD user as an Identity user so we can relate a role to AD Login.
                var chkUser = UserManager.Create(newUser, model.Password);
                ModelState.AddModelError("", "Created new user: " + model.UserName + " Error: " + string.Join("\n", chkUser.Errors.ToArray()));
    
                //Add default User to Role Admin   
                if (chkUser.Succeeded)
                {
                    ModelState.AddModelError("", "Created Identity user: " + newUser.UserName);
                    var result1 = UserManager.AddToRole(newUser.Id, "PM");
                    if (result1.Succeeded)
                        ModelState.AddModelError("","Added PM role to user: " + newUser.UserName);
                }
            }
        }
    
陷阱包括:

  • IdentityConfig.cs文件中的密码验证与AD的密码验证标准不匹配
  • 没有将我的AppPool的标识设置为LocalSystem

共享视图_LoginPartial.cshtml和_Layout.cshtml已正确编码。

这取决于启动项目时选择的内容。如果您选择active directory,这将很困难,因为在初始选择之后,MVC项目中的身份验证很难修改。