Identity icon indicating copy to clipboard operation
Identity copied to clipboard

What is the best method to couple aspnet/Identity to local ActiveDirectory?

Open WillBaldwin opened this issue 11 years ago • 3 comments

What I want to do is authenticate users to ActiveDirectory. It would be ideal to be able to use windows Authentication but I'll settle with forms authentication if it works. I have a working function to check the credentials of a user using LDAP (see below). What I need is all the bits that are performed by SignInManager.PasswordSignInAsync(). Now I could write this code; but this use case seems to be general for any enterprise developer deploying on IIS. At this point it seems easier to use Identity Framework to authenticate my app against (choke) Twitter than it is my local Active Directory Domain.

What is the best method to couple aspnet/Identity to local ActiveDirectory? Does anyone have a solution I have not found yet?

Goals:

  1. Authenticate users to local ActiveDirectory
  2. Use Identity Framework to manage claims

Fails:

  1. Microsoft.Owin.Security.ActiveDirectory: This package only works with Azure Active Directory (AAD) and Active Directory Federation Services (ADFS) via WsFederation. There is no support for LDAP! In my opinion they really need to rename this project to Microsoft.Owin.Security.ActiveDirectoryButNachoActiveDirectory
  2. Windows Authentication: Windows Authentication has major side-effects such as tokens and claims are all managed by AD group memberships. This doesn't scale well if a corporate AD supports 5 internal applications it may need 50 groups or more to manage the claims. Windows Authentication does not generally play well with Identity Framework. I need to decouple authentication from authorization so claims can be managed inside the app and apparently I can not do this with Windows Authentication the way Identity Framework is designed now.

Code

    public static bool ValidateCredentials(string domainController, int port, string domain, string username, string password)
    {
        const int LDAP_InvalidCredentials = 0x31;

        LdapDirectoryIdentifier ldapDirectoryIdentifier
            = new LdapDirectoryIdentifier(domainController, port);

        NetworkCredential networkCredential
            = new NetworkCredential(username, password, domain);

        try
        {
            using (LdapConnection ldapConnection = new LdapConnection(ldapDirectoryIdentifier))
            {
                ldapConnection.SessionOptions.SecureSocketLayer = true;
                ldapConnection.AuthType = AuthType.Negotiate;
                ldapConnection.Bind(networkCredential);
            }

            // if the bind succeeds, the credentials are OK
            return true;
        }
        catch (LdapException ldapException)
        {
            // Unfortunately, invalid credentials fall into this block with a specific error code
            if (ldapException.ErrorCode.Equals(LDAP_InvalidCredentials)) return false;
            throw;
        }

    }

WillBaldwin avatar Mar 05 '15 21:03 WillBaldwin

This following post pushed me toward a direction that worked, the code in the post fails, but I was able to start from that and get to where I had working solution:

http://stackoverflow.com/questions/28888006/how-to-use-windows-active-directory-authentication-and-identity-based-claims/28891029?noredirect=1#comment46068537_28891029

NOTE Identity Framework Team: The following solution works, but when writing for enterprise applications where 100% of all users are windows users. It would be ideal to use Windows Authentication for the authentication piece and use Identity Framework for claims management. Why? (1) Windows Authentication doesn't require the user to type in credentials. (2) Windows Authentication doesn't pass passwords over the network. (3) Claims for applications are often managed by people who do not have group management privileges in Active Directory.

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        //
        // Check for user existance in Identity Framework
        //
        ApplicationUser applicationUser = await _userManager.FindByNameAsync(model.eID);
        if (applicationUser == null)
        {
            ModelState.AddModelError("", "Invalid username");
            return View(model);
        }

        //
        // Authenticate user credentials against Active Directory
        //
        bool isAuthenticated = await Authentication.ValidateCredentialsAsync(
            _applicationSettings.Options.DomainController, 
            _applicationSettings.Options.DomainControllerSslPort, 
            model.eID, model.Password);
        if (isAuthenticated == false)
        {
            ModelState.AddModelError("", "Invalid username or password.");
            return View(model);
        }

        //
        // Signing the user step 1.
        //
        IdentityResult identityResult 
            = await _userManager.CreateAsync(
                applicationUser, 
                cancellationToken: Context.RequestAborted);

        if(identityResult != IdentityResult.Success)
        {
            foreach (IdentityError error in identityResult.Errors)
            {
                ModelState.AddModelError("", error.Description);
            }
            return View(model);
        }

        //
        // Signing the user step 2.
        //
        await _signInManager.SignInAsync(applicationUser,
            isPersistent: false,
            authenticationMethod:null,
            cancellationToken: Context.RequestAborted);

        return RedirectToLocal(returnUrl);
    }

WillBaldwin avatar Mar 06 '15 18:03 WillBaldwin

Yeah so I prototyped this early on in Identity v1, basically you would treat AD login as a separate flow but similar to local password login, so the answer you got on SO that works is basically that. Instead of check username/password via the sign in manager's PasswordSignIn, you are basically implementing the spiritual equivalent of SignInManager.ADSignIn if it existed

HaoK avatar Mar 06 '15 19:03 HaoK

So is it considered as a good practice to use the LDAP authentication(Authentication.ValidateCredentialsAsync) inside the account controller itself besides the usermanger authentication(_userManager.FindByNameAsync)? Or it would be better if I inherited a new class from the usermanger and modified the FindByNameAsync method to do the LDAP piece as well to encapsulate the authentication logic in one place?

azeyad avatar Oct 26 '15 16:10 azeyad