【问题标题】:Authenticate against AD LDS针对 AD LDS 进行身份验证
【发布时间】:2012-03-06 04:45:51
【问题描述】:

我刚刚在我的开发人员 PC 上安装了 AD LDS,一切正常,我什至通过 ADSI Edit 创建了用户“abc”。

我的目标是使用我的测试 AD LDS 实例测试我的 ASP.NET Mvc 3 Web 应用程序。

如何让应用针对实例对用户进行身份验证?我必须编写自定义会员提供程序吗? (覆盖默认 AD 成员资格提供程序中的一些内容?)

感谢您的帮助!

【问题讨论】:

  • 您不需要编写自定义提供程序。您可以使用任何其他 LDAP 工具连接到它吗?

标签: asp.net asp.net-mvc asp.net-mvc-3 active-directory


【解决方案1】:

您不必进行任何身份验证,因为它由 iis 处理。 您所要做的就是将身份验证模式更改为 windows。

   <system.web>
      <authentication mode="Windows" />
   </system.web>

请记住在安装 AD 之后安装 iis,或者手动注册。

【讨论】:

    【解决方案2】:

    因为您使用的是 AD LDS,所以我认为身份验证模式“Windows”不会有太大帮助。我相信您需要创建一个登录视图(此处为 /Account/Logon)并使用身份验证模式“表单”。

    在 web.config 中输入以下内容

    <authentication mode="Forms">
      <forms name=".ADAuthCookie" loginUrl="~/Account/Logon" timeout="30" slidingExpiration="false" protection="All"/>
    </authentication>
    
    <authorization>
      <deny users="?"/>
    </authorization>    
    

    可以使用 System.DirectoryServices.AccountManagement 对用户进行身份验证。控制器代码应如下所示:

    public ActionResult Logon(LogonModel model)
    {
        if (model.Username != null && model.Password != null)
        {
    
            string container = "CN=...,DC=....,DC=...."; //Your container in LDS 
            string ldapserver = "server:port"; //LDS server
    
            PrincipalContext context = new PrincipalContext(
                ContextType.ApplicationDirectory, 
                ldapserver, 
                container, 
                ContextOptions.SimpleBind);
    
    
            bool authenticate = context.ValidateCredentials(string.Format("CN={0},{1}", model.Username, container), model.Password, ContextOptions.SimpleBind);
    
            if (authenticate)
            {
                FormsAuthentication.RedirectFromLoginPage(model.Username, false);
            }
            else
            {
                System.Threading.Thread.Sleep(5000);
                this.ModelState.AddModelError("Password", "Wrong username or password");
            }
        }
    
        return View("Logon", new LogonModel { Username = model.Username });
    }
    

    请注意,这只解决了身份验证问题,而不是授权问题。

    您也可以使用会员提供商,但如果您正在寻找一个简单的解决方案,我认为这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多