[英]ASP.NET MVC LDAP Authentication
我一直试图通过LDAP对我徒劳开发的新应用程序进行身份验证。 我一直能够在其他应用程序上执行此操作,但是由于某种原因,它只是无法执行。 我打开其他应用程序并尝试复制几乎相同的代码,但我一直遇到相同的问题。 如果身份验证成功,该应用程序将重定向到该页面,但是当我尝试导航到另一个具有[Authorize]
功能的页面时,需要我回到登录页面。 我徒劳地尝试检查问题可能是什么,但仍然找不到。 请协助。 这是我的部分代码
//Accounts controller
[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
string domain = (string)model.domain
string userName = (string)model.UserName;
string password = (string)model.Password;
try
{
DirectoryEntry entry = new DirectoryEntry();
switch (domain)
{
case "OPTION1":
entry = new DirectoryEntry("LDAP://xx.xx.xx.xx:389", userName, password);
break;
case "OPTION2":
entry = new DirectoryEntry("LDAP://yy.yy.yy.yy:389", userName, password);
break;
}
object nativeObject = entry.NativeObject;
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
}
return View(model);
}
//web.config
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="60" slidingExpiration="false" protection="All" />
</authentication>
//in AccountViewModels I have
using System.ComponentModel.DataAnnotations;
public class LoginViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
我在这里看到的问题是您没有验证用户。 例如
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "Home");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.