MVC5 OWIN 广告和数据库

MVC5 OWIN ad and database

我正在创建 MVC5 应用程序。这是一个内联网应用程序。所有用户都已通过本地 Active Directory 域的身份验证。

我们有一个当前用于 Windows 应用程序的现有数据库。

我想获取用户的域登录名并使用它来查找已在该数据库中配置的角色和声明。

我假设 ASP.NET / MVC5 / Authentication "Individual User Accounts" 的基础项目是起点。

请指出正确的方向。

谢谢

.NET OWIN Identity classes 要求您通过 ApplicationUserManager class 的 CheckPasswordAsync() 方法进行身份验证。这可以通过覆盖 class ApplicationUserManager 的 CheckPasswordAsync() 方法来完成。在您的覆盖中,您将需要调用 class System.DirectoryServices.AccountManagement 的 ValidateCredentials() 方法以通过 Active Directory 进行身份验证。这将要求用户使用他们的 Windows 用户名和密码登录应用程序。不过,有几个步骤可以让它发挥作用。

正如您所说,您从一个具有 "Individual User Accounts" 身份验证的基础项目开始。

步骤 1 - 通过将以下代码添加到 ConfigureAuth() 方法来更新文件 App_Start\Startup.Auth.cs 中的 ConfigureAuth() 方法。

using System.DirectoryServices.AccountManagement;

//Add an Owin context for Active Directory principals
app.CreatePerOwinContext(() => new PrincipalContext(ContextType.Domain));

其余更新在文件 App_Start\IdentityConfig.cs

中完成

步骤 2 - 更新 class ApplicationUserManager.

的构造函数
using System.DirectoryServices.AccountManagement;

//Add a PrincipalContext parameter to the constructor
public ApplicationUserManager(IUserStore<ApplicationUser> store, PrincipalContext principal) : base(store)
{
    this.principal = principal;
}

第 3 步 - 在 Create() 方法中更新对 class ApplicationUserManager.

构造函数的调用
//Add the PrincipalContext parameter
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<PortalIdentityDbContext>()), context.Get<PrincipalContext>());

步骤 4 - 覆盖 class ApplicationUserManager 的 CheckPasswordAsync() 方法。

//Override CheckPasswordAsync to login via Active Directory.
public override async Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
{
    return await Task.FromResult(this.principal.ValidateCredentials(user.UserName, password, ContextOptions.Negotiate));
}

至于使用现有数据库,您必须将 OWIN Identity 表合并到其中,反之亦然。 Identity 功能需要这些表,您无法更改它。我会创建一个测试项目并熟悉这些表。然后弄清楚您希望如何将它们合并到现有数据库中,反之亦然。我为我的自定义功能大量修改了这些表。但是核心表和列必须存在。

您不需要完整的 ASP.Net 身份。相反,您可以只使用 OWIN cookie 身份验证中间件。

通过 Active Directory 验证凭据

public bool ValidateCredentials(string userName, string password)
{
   using (var context = new PrincipalContext(ContextType.Domain))
   {
      return context.ValidateCredentials(userName, password);
   }
}

通过旧数据库授权

通过身份验证后,您想从旧数据库中检索授权角色,并创建声明。

private readonly HttpContextBase _context;
private const string AuthenticationType = "ApplicationCookie";

public OwinAuthenticationService(HttpContextBase context)
{
    _context = context;
}

public void SignIn(User user)
{
    IList<Claim> claims = new List<Claim>
    {
        new Claim(ClaimTypes.Sid, user.Id.ToString()),
        new Claim(ClaimTypes.Name, user.UserName),
        new Claim(ClaimTypes.GivenName, user.FirstName),
        new Claim(ClaimTypes.Surname, user.LastName),
    };

    // Get authorized roles from old database
    foreach (Role role in user.Roles)
    {
        claims.Add(new Claim(ClaimTypes.Role, role.Name));
    }

    ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

    IOwinContext context = _context.Request.GetOwinContext();
    IAuthenticationManager authenticationManager = context.Authentication;

    authenticationManager.SignIn(identity);
}

public void SignOut()
{
    IOwinContext context = _context.Request.GetOwinContext();
    IAuthenticationManager authenticationManager = context.Authentication;

    authenticationManager.SignOut(AuthenticationType);
}

Startup.cs

您还需要为所有这些配置启动。

[assembly: OwinStartup(typeof(YourApplication.Startup))]
namespace YourApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "ApplicationCookie",
                LoginPath = new PathString("/Account/Login")
            });
        }
    }
}

希望你能找到起点。