C# 成功登录后的ajax重定向
我一直在互联网上寻找答案。我试图添加一个jquery对话框窗口,该窗口将调用一个操作来登录用户,然后在成功登录后,将用户重定向到其配置文件页面,否则将保持对话框窗口打开,并向用户提示相应的错误消息。到目前为止,登录部分似乎工作正常,但当操作返回时,它会将用户保持在同一页面上。我需要做哪些更改才能确定成功登录并正确重定向?这是我的密码:C# 成功登录后的ajax重定向,c#,asp.net,asp.net-mvc-3,asp.net-ajax,C#,Asp.net,Asp.net Mvc 3,Asp.net Ajax,我一直在互联网上寻找答案。我试图添加一个jquery对话框窗口,该窗口将调用一个操作来登录用户,然后在成功登录后,将用户重定向到其配置文件页面,否则将保持对话框窗口打开,并向用户提示相应的错误消息。到目前为止,登录部分似乎工作正常,但当操作返回时,它会将用户保持在同一页面上。我需要做哪些更改才能确定成功登录并正确重定向?这是我的密码: "Javascript code" $.validator.unobtrusive.parse('#LogOnForm'); $('#LogOnDialog').
"Javascript code"
$.validator.unobtrusive.parse('#LogOnForm');
$('#LogOnDialog').dialog({
autoOpen: false, width: 450, height: 300, modal: true,
buttons: {
'Log On': function () {
if ($('#LogOnForm').validate().form()){
$.ajax({
url: '@Url.Action("LogOnPartial", "Account")',
type: 'POST',
data: $('form').serialize(),
datatype: 'json',
success: function (result) {
$('#LogOnDialog').html(result).dialog('open');
}
});
}
},
Cancel: function () {
$(this).dialog('close');
}
}
});
$('#linkSignIn').live('click', function () {
$('#LogOnDialog').html('')
.dialog('option', 'title', 'Sign In')
.load('@Url.Action("LogOnPartial", "Account")', function () { $('#LogOnDialog').dialog('open'); });
});
"Controller Action"
[HttpPost]
public ActionResult LogOnPartial(LogOnModel model)
{
if (ModelState.IsValid)
{
UserPrincipal principal = new UserPrincipal(model.EmailAddress, model.Password);
HttpContext.User = principal;
FormsAuthentication.SetAuthCookie(model.EmailAddress, true);
return PartialView("LogOnPartial", model);
}
return PartialView("LogOnPartial", model);
}
我不知道你想如何实现这一点,但在你的结果,你最有可能想返回的用户id的配置文件,你想登录
success: function (result) {
if(result=='')/// no result show the dialog again
{
$('#LogOnDialog').html(result).dialog('open');
}
else // redirect to profile page
{
window.location = 'profile/'+result;
}
}
});
你的行为可以是
public ActionResult ProvinceFilter(LogOnModel model)
{
string result=="";
UserPrincipal principal = new UserPrincipal(model.EmailAddress, model.Password); //in order to retorn exact error you must modify the principle to check if the user is valid or not and return specific error
if(principal==null) //or not valid
{
result="Your Username or Password is not correct";
}
else
{
HttpContext.User = principal;
FormsAuthentication.SetAuthCookie(model.EmailAddress, true);
result=principal.UserID.ToString();
}
return Json(result);
}
要区分这两种情况,我的操作应该是什么样子的?您需要使用ajax操作/json返回从上面的操作判断,无论ModelState.Valid是否为true,我是否应该返回这个值?如果为false,我希望用户看到带有一些错误消息的模式面板,让他们知道他们做错了什么,无效的用户/过程组合,等等,否则执行重定向。您可以,但是我只会从操作返回一个纯错误消息。如果我返回一个错误消息,你能改变我上面的操作以匹配它的外观吗?无法想象上面我的ActionResult应该是什么样子。