Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC LDAP身份验证_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC LDAP身份验证

C# ASP.NET MVC LDAP身份验证,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我一直在尝试通过LDAP在我正在开发的新应用程序上进行身份验证,但徒劳无功。我一直能够在其他应用程序上执行此操作,但这次由于某些原因,它无法通过。我打开我的其他应用程序,试图复制几乎相同的代码,但我一直遇到同样的问题。如果身份验证成功,应用程序将重定向到它应该显示的页面,但当我尝试导航到另一个页面时,在该页面之前我有一个带有[Authorize]的功能,它会将我带回到登录页面。我试图检查问题可能是什么,但还是找不到。请帮忙。这是我代码的一些部分 //Accounts controller [Ht

我一直在尝试通过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; }
}
//帐户控制器
[HttpPost]
公共操作结果登录(LoginView模型,字符串返回URL)
{
字符串域=(字符串)model.domain
字符串用户名=(字符串)model.userName;
字符串密码=(字符串)model.password;
尝试
{
DirectoryEntry=新的DirectoryEntry();
交换机(域)
{
案例“选项1”:
entry=newdirectoryentry(“LDAP://xx.xx.xx.xx:389”,用户名,密码);
打破
案例“选择2”:
entry=newdirectoryentry(“LDAP://yy.yy.yy:389”,用户名,密码);
打破
}
object nativeObject=entry.nativeObject;
FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
if(this.Url.IsLocalUrl(returnUrl)&&returnUrl.Length>1&&returnUrl.StartsWith(“/”)
&&!returnUrl.StartsWith(“//”&!returnUrl.StartsWith(“/\”)
{
返回this.Redirect(returnUrl);
}
返回此.RedirectToAction(“索引”、“主目录”);
}
捕获(例外情况除外)
{
this.ModelState.AddModelError(string.Empty,“提供的用户名或密码不正确”);
}
返回视图(模型);
}
//web.config
//在AccountViewModels中,我有
使用System.ComponentModel.DataAnnotations;
公共类LoginView模型
{
[必需]
[显示(Name=“User Name”)]
公共字符串用户名{get;set;}
[必需]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[显示(Name=“记得我吗?”)]
公共布尔记忆{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");
}

我在这里看到的问题是您没有验证用户。 例如

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