如何在MVC 5中创建LDAP用户身份验证登录页面

时间:2018-08-25 11:06:55

标签: authentication asp.net-mvc-5 active-directory ldap azure-active-directory

我正在尝试实现一个使用LDAP针对活动目录对用户进行身份验证的应用程序。 我已经在Internet上进行了搜索,以找到解决此问题的方法,但是我找不到。这是我尝试过的方法,我不知道为什么 SearchResult user = userSearcher.FindOne()返回Null?我究竟做错了什么?

  

我尝试查看LINK,但不涉及LDAP用户身份验证。

方法:

public ELdapState Authenticate(string domain, string username, string pwd)
            {
                string domainAndUsername = $"{domain}\\{username}";

                try
                {
                    using (this._entry = new DirectoryEntry(this._path))
                    {
                        this.State = ELdapState.NotAuthenticated;
                        using (DirectorySearcher userSearcher = new DirectorySearcher(this._entry,
                         $"(&(objectCategory=Person)(objectClass=user)(mail=*)(sAMAccountName={username}))"))
                        {
                            userSearcher.PropertiesToLoad.Add("cn");
                            userSearcher.PropertiesToLoad.Add("sn");
                            userSearcher.PropertiesToLoad.Add("title");
                            userSearcher.PropertiesToLoad.Add("description");
                            userSearcher.PropertiesToLoad.Add("givenDame");
                            userSearcher.PropertiesToLoad.Add("mail");
                            userSearcher.PropertiesToLoad.Add("company");
                            userSearcher.PropertiesToLoad.Add("mobile");
                            userSearcher.PropertiesToLoad.Add("accountExpires");
                            userSearcher.PropertiesToLoad.Add("memberOf");

                            SearchResult user = userSearcher.FindOne();
                            if (user == null)
                            {
                                this.State = ELdapState.AccountNotFound;

                            }
                        }
                     }
                  }
               }

操作:

   [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!Request.IsAuthenticated)
            {
                if (ModelState.IsValid)
                {

                    Ldap nclss = new Ldap(returnUrl);

                    if (nclss.Authenticate(model.Domain, model.Email, model.Password) != 0)
                    {

                        if (!String.IsNullOrEmpty(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Dashboard");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The Email Address or password provided is incorrect.");
                    }

                }


            }
            return View();
        }

查看:

  @using (Html.BeginForm("Login", "Login", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                        {
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true)
                                    <div class="p-t-31 p-b-9">
                                        <span class="txt1">
                                            Username
                                        </span>
                                    </div>
                                    <div class="wrap-input100 validate-input" data-validate="Username is required">
                                         @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control input100" } })
                                         @Html.ValidationMessageFor(model => model.Email)
                                        <span class="focus-input100"></span>
                                    </div>

                                      <div class="wrap-input100 validate-input" data-validate="Username is required">
                                         @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control input100" } })
                                        @Html.ValidationMessageFor(model => model.Password)
                                        <span class="focus-input100"></span>
                                    </div>

                                    <div class="container-login100-form-btn m-t-17">
                                        <button class="login100-form-btn" type="submit">
                                            Sign In
                                        </button>
                                    </div>

                        }

我被困在这里。任何帮助将不胜感激!

0 个答案:

没有答案