MVC 4 authentication with Active Directory or Membership database

我只是一个虾纸丫 提交于 2019-12-03 14:24:29

Open up your web.config.

First of all you'll need connectionString for your ActiveDirectory:

  <connectionStrings>
    ...
    <add name="ADConnectionString" connectionString=LDAP://*adserver*/DC=*domain* />
    ...
  </connectionStrings>

Scroll down to the <membership> tag. Make sure you have defaultProvider attribute set for the <membership>, like:

<membership defaultProvider="SimpleMembershipProvider">

Then add new provider for AD members inside <providers>:

    <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />

That should do the trick for web.config. Now we need to auth AD users on Log in. Go to your AccountController Login action. First we try to authenticate user via ActiveDirectory, there is handy class called PrincipalContext in System.DirectoryServices.AccountManagement namespace. If that fails we use the default membership provider:

        public ActionResult Login(LoginModel model, string returnUrl)
        {
            try
            {
                // try to auth user via AD
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
                {
                    if (pc.ValidateCredentials(model.UserName, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, false);
                        return RedirectToAction("Index", "Home");
                    }
                }
                // try the default membership auth if active directory fails

                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Login failed");
                }
            }
            catch
            {
            }
            GetErrorsFromModelState();
            return View(model);
        }

For your later requirements you can get the current logged in ActiveDirectory user with UserPrincipal class:

using (var context = new PrincipalContext( ContextType.Domain)) 
{
    using (var aduser = UserPrincipal.FindByIdentity( context,IdentityType.SamAccountName, HttpContext.User.Identity.Name))
    {
        ...
    }
}

Hope this helps and I didn't miss anything.

This Code will give you if the user with specified username and password is valid

    public bool ValidateUser(string userName, string password)
    {
        bool authenticated = false;
        string dePath = string.Empty;
        dePath += DomainController;
        if (!string.IsNullOrEmpty(BaseDomainName))
        {
            dePath += "/" + BaseDomainName; 
        }
        try
        {
            DirectoryEntry entry = new DirectoryEntry(dePath, userName, password);
            object nativeObject = entry.NativeObject;
            authenticated = true;
        }
        catch
        {
            return false;
        }
        return authenticated;
    }

You can add DomainController and BaseDomainName in web.config appSettings as keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!