首页 > 解决方案 > Galactic.ActiveDirectory 在 ASP MVC 中不起作用

问题描述

使用以下代码,我没有错误,但所有登录均因登录错误而失败。

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;

我正在使用包Galactic.ActiveDirectory

这是控制器的一部分:

[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);
    }
}

catch 块释放,变量有如下值。

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

我尝试了几种组合,但都不起作用。

带域名:

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

没有域名和反斜杠

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

.. 等等。

\\您可以在字符串变量中看到双反斜杠。我认为这属于转义序列,这不是错误的原因吗?

任何人都可以帮助我或知道问题出在哪里吗?

标签: c#asp.netasp.net-mvc-5

解决方案


这不是服务器名称,而是域名

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

ActiveDirectory 构造函数(字符串、字符串、SecureString、字符串)

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)

推荐阅读