【发布时间】:2017-09-28 15:14:48
【问题描述】:
我有一个非常简单的登录页面。问题在于登录方法。我执行 RedirectToAction,但是,它永远不会将网页从登录页面更改为主页。
HomeController(主应用控制器)
public ActionResult Index(string username = null, string password = null)
{
if (username == null || password == null)
{
return RedirectToAction("Index", "Auth");
}
this.username = username;
this.password = password;
return View(); //and have tried return View("Index");
}
AuthController(获取用户名和密码)
public ActionResult Login(string username, string password)
{
return RedirectToAction("Index","Home", new { username=username,password=password});
}
按钮按下
function Login() {
var _get_searchdetails = '@Url.Action("Login", "Auth")';
$.ajax({
url: _get_searchdetails,
data: { username: $('#username').val(), password: $('#password').val() },
type: "POST",
beforeSend: function () {
},
error: function (xhr, textStatus, error) {
alert("Try again!");
},
success: function (htmldata, xhr, settings) {
}
});
}
【问题讨论】:
-
type: "GET",* GASP * -
在控制器上设置字段是行不通的,因为控制器被实例化并随每个请求处理。您发出的下一个请求
username和password将再次为空。 -
是的,我保存了这些... Session["username"] = login.username; AND Session["password"] = login.password;不过谢谢!
-
您还应该使用
[HttpPost]属性和[ValidateAntiForgeryToken]来装饰Login方法(假设您还在表单中输入了@Html.AntiForgeryToken())。 -
感谢您的提示,我最关心的是让某些东西正常工作,因为它是一个受证书保护的内部应用程序。
标签: asp.net-mvc razor