针对AD LDS进行身份验证
问题描述:
我刚刚在我的开发人员PC上安装了AD LDS,并且所有工作都可以找到,我甚至通过ADSI Edit创建了用户“abc”。针对AD LDS进行身份验证
我的目标是用我的测试AD LDS实例测试我的ASP.NET Mvc 3 Web应用程序。
我该如何获得应用程序以针对实例验证用户身份?我必须写一个自定义会员供应商吗? (覆盖默认的AD会员供应商的一些东西?)
谢谢你的帮助!
答
您不必进行任何身份验证,因为它是由iis处理的。 您只需将认证模式更改为Windows即可。
<system.web>
<authentication mode="Windows" />
</system.web>
记住要么安装IIS 后您安装了广告,或手动注册。
答
因为您正在使用AD LDS,所以我认为认证模式“Windows”不会如此有用。我相信你需要创建一个登录视图(在这里/帐户/登录)并使用身份验证模式“表单”。
web.config中
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Account/Logon" timeout="30" slidingExpiration="false" protection="All"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
验证用户输入followwing可以通过使用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 });
}
请注意,这只能解决认证和不授权。
你也可以使用会员供应商,但如果你正在寻找一个简单的解决方案,我认为这应该做的伎俩。
您不需要编写自定义提供程序。您可以使用任何其他LDAP工具连接到它吗? – 2012-11-15 17:42:22