我尝试使用PricipalContext(ContextType.Domain)成功进行Active Directory登录后重定向回我的主页/索引页面以验证凭据。我可以看到证书已成功验证,但页面似乎只是重新加载并清除我的用户名和密码,而不是重定向到指向我的家庭控制器的索引页面。我看到的一切意味着使用返回RedirectToAction("索引"," Home")但似乎并不适合我。
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
答案 0 :(得分:1)
authorize属性要求设置表单授权cookie。用户成功登录后,您需要致电:
FormsAuthentication.SetAuthCookie(userInfo[1], false);
请参阅方法签名上的MSDN documentation。另一个有用的读物:What does FormsAuthentication.SetAuthCookie do。