2017-04-19 90 views
0

我试图在使用PricipalContext(ContextType.Domain)验证凭据成功Active Directory登录后重定向回我的Home/Index页面。我可以看到证书正在成功验证,但是页面似乎重新加载并清除了我的用户名和密码,而不是重定向到与我的主控制器对应的索引页面。我看到的一切都意味着使用return RedirectToAction(“Index”,“Home”),但似乎不适用于我。return RedirectToAction没有从登录页面重定向页面

web.config中 - 你可以看到,它在默认情况下

<authentication mode="Forms"> 
    <forms loginUrl="~/Login" timeout="2080" cookieless="UseCookies"></forms> 
</authentication> 

的HomeController

[Authorize] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

的LoginController

[HttpGet] 
public ActionResult Index() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(FormCollection form) 
{ 
string user = form["user"] as string; 
string password = form["password"] as string; 
string[] userInfo = user.Split('/'); 

if ((userInfo.Length > 2) || (userInfo.Length < 2)) 
{ 
    ViewBag.Message = "You must enter Domain and Username in Domain/User format."; 
    return View("Index"); 
} 
else 
{ 
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, userInfo[0])) 
{ 
    bool isValid = pc.ValidateCredentials(userInfo[1], password); 

    if (isValid) 
    { 
here. 
     return RedirectToAction("Index", "Home"); 
    } 
    else 
    { 
     ViewBag.Message = "Invalid Credentials. Maximum of 3 attempts before lock out."; 
     return View("Index"); 
    } 
} 
} 
} 

带您到登录页面,如果你需要我提供我可以做任何事情。我知道有一些我在这里做错了,但我不明白为什么它保持重定向回登录,而不是〜/ Home/Index

回答

1

authorize属性需要设置窗体授权cookie。当用户成功登录后,你需要调用:

FormsAuthentication.SetAuthCookie(userInfo[1], false); 

看到MSDN documentation的方法的签名。另外一个有用的阅读:What does FormsAuthentication.SetAuthCookie do

+0

是的,工作。谢谢。所以基本上只是等待授权来设置一个cookie。我将阅读您提供的文档以了解更多信息。再次感谢。 – Tim