How to use Windows Active Directory Authentication and Identity Based Claims?

前端 未结 5 1963
一向
一向 2020-12-07 12:21

Problem

We want to use Windows Active Directory to authenticate a user into the application. However, we do not want to use Active Directory groups to manage autho

5条回答
  •  自闭症患者
    2020-12-07 12:54

    Just hit AD with the username and password instead of authenticating against your DB

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.UserName);
            if (user != null && AuthenticateAD(model.UserName, model.Password))
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }
        return View(model);
    }
    
    public bool AuthenticateAD(string username, string password)
    {
        using(var context = new PrincipalContext(ContextType.Domain, "MYDOMAIN"))
        {
            return context.ValidateCredentials(username, password);
        }
    }
    

提交回复
热议问题