Search code examples
c#asp.netasp.net-mvc-5

Galactic.ActiveDirectory doesn't work at ASP MVC


With the following code, I have no errors but all logins fail with incorrect login.

using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MVCFBAWithAD.Models;
using System.Security;
using System.Configuration;
using Galactic.ActiveDirectory;
using System.DirectoryServices.Protocols;
using System.Security.Principal;

I'm using the package Galactic.ActiveDirectory.

And this is part of the controller:

[Authorize]
public class AccountController : Controller
{
    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        string serverName = ConfigurationManager.AppSettings["ADServer"];

        if (ModelState.IsValid)
        {
            SecureString securePwd = null;

            if (model.Password != null)
            {
                securePwd = new SecureString();

                foreach (char chr in model.Password.ToCharArray())
                {
                    securePwd.AppendChar(chr);
                }
            }

            try
            {
                // Check user credentials
                ActiveDirectory adVerifyUser = new ActiveDirectory(serverName, model.UserName, securePwd);

                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            catch
            {
                // If we got this far, something failed, redisplay form
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        return View(model);
    }
}

The catch block release, and the variables has following value.

ActiveDirectory adVerifyUser = new ActiveDirectory(serverName, model.UserName, securePwd)

I tried several combinations and it doesn't work.

With domain name:

serverName =  \\\\SCD1000
model.UserName =  DOMAINNAME\\Dirk
securePWD = {System.Security.SecureString}

Without domain name and backslash

serverName =  SCD1000
model.UserName = Dirk
securePWD = {System.Security.SecureString}

.. and so on.

You can see a double backslash \\ in the string variable. I think this belongs to the escape sequences and this is not the reason of the error?

Can anybody help me or knows where the problem lies?


Solution

  • It's not the serverName, its the DOMAIN Name

    http://galacticapi.github.io/documentation/html/508a11b5-fa71-664c-10f0-15fab368078b.htm

    ActiveDirectory Constructor (String, String, SecureString, String)

    domainName
    
    Type: System.String
       The DNS style domain name of the Active Directory to connect to.
    userName
       Type: System.String
       The username of the account in AD to use when making the connection.
    password
       Type: System.Security.SecureString
       The password of the account.
    siteName (Optional)