这是我的登录视图代码,通过它我向帐户控制器发送ajax呼叫
$(document).ready(function () {
$('#save').on('click', function () {
debugger
var Email = $("#email").val();
var Password = $("#password").val();
if ($("#rememberme").is(":checked")) {
var Rememeberme = true;
}
debugger
$.ajax({
type: "POST",
url: '/Account/Login',
cache: false,
data: { EmailId: Email, Password: Password, Rememeberme: Rememeberme },
success: function (response)
{
debugger
alert("User Login Successfully");
window.location.href = response.Url;
//window.location = '/Home/Index';
},
error: function ()
{
debugger
alert("error unSuccessfully");
//<div class="alert alert-success">
// <strong>Error!</strong>Invalid USer</div>
}
});
});
});
我做了两个控制器一个用于登录(AccountController),另一个用于Admin(HomeController)。登录后我想重定向到HomeController和Index动作,但它不起作用。我的代码如下。
//Account Controller For Login
[HttpPost]
public ActionResult Login(Login lg, string ReturnUrl = "")
{
string message = "";
using (PresidencyDBEntities1 dc = new PresidencyDBEntities1())
{
var v = db.UserTbls.Where(a => a.EmailID == lg.EmailID).FirstOrDefault();
if (v != null)
{
//if (!v.IsEmailVerified)
//{
// ViewBag.Message = "Please verify your email first";
// return View();
//}
if (string.Compare(Crypto.Hash(lg.Password), v.Password) == 0)
{
int timeout = lg.RememberMe ? 525600 : 20; // 525600 min = 1 year
var ticket = new FormsAuthenticationTicket(lg.EmailID, lg.RememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(ReturnUrl))
{
return Redirect(ReturnUrl);
}
else
{
//var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
//return Json(new { Url = redirectUrl ,success = true, message = "Saved Succesfully" }, JsonRequestBehavior.AllowGet);
return RedirectToAction("Index", "Home");
// return Json(new { success = true, message = "Saved Succesfully" }, JsonRequestBehavior.AllowGet);
}
}
else
{
message = "Invalid credential provided";
}
}
else
{
message = "Invalid credential provided";
}
}
ViewBag.Message = message;
return View();
}
<!-- end snippet -->
成功登录操作后,我想在此控制器中重定向
[Authorize]
public class HomeController : Controller
{
PresidencyDBEntities1 db = new PresidencyDBEntities1();
public ActionResult Contact()
{
return View();
}
public ActionResult Index()
{
List<SelectListItem> genr = db.TypeTbls.Select(n =>
new SelectListItem
{
Value = n.Type_ID.ToString(),
Text = n.Name
}).ToList();
ViewBag.SelectItem = genr;
return View();
}
答案 0 :(得分:0)
你的ajax代码看起来是正确的。我只是在登录帖子方法上做了一些改动。
[HttpPost]
public JsonResult Login(string user, string pass, bool rememberme)
{
if ()// login success
{
status.Success = true;
status.Message = "Login successful!";
status.successURL = Url.Action("Index","Home");
}
else
{
status.Success = false;
status.Message = "Invalid UserID or Password!";
status.successURL = "";
}
return Json(status);
}
的Ajax。
success: function (response)
{
if(response.Status)
{
window.location.href = response.successURL;
}
else{
alert(response.Message);
}
},
error: function ()
{
debugger
alert("error unSuccessfully");
//<div class="alert alert-success">
// <strong>Error!</strong>Invalid USer</div>
}