2016-04-19 30 views
1

私は無駄に開発している新しいアプリケーションでLDAP経由で認証しようとしています。私はいつも他のアプリケーションでこれを行うことができましたが、今度は何らかの理由で今回はやりません。私は他のアプリを開き、コードをほぼ同じに複製しようとしますが、私は同じ問題を抱えています。アプリケーションは、認証が成功した場合にページをリダイレクトしますが、その前に[Authorize]の機能を持つ別のページに移動しようとすると、ログインページに戻ります。私は問題が何であるかを調べることを無駄に試みたが、それでもそれを見つけることはできない。手伝ってください。ここに私のコードのいくつかの部分がありますASP.NET MVC LDAP認証

//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; } 
} 

答えて

0

ここで私が見ることができる問題は、あなたがユーザーを検証していないことです。

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