Asp.net mvc ASP.NET MVC在登录后重定向到目标页或重定向到上一页
我在使用asp.net MVC时遇到以下问题:当管理员更新用户请求时,他将收到一封电子邮件,告知请求已被更新,并且在这封电子邮件中会有指向页面Asp.net mvc ASP.NET MVC在登录后重定向到目标页或重定向到上一页,asp.net-mvc,Asp.net Mvc,我在使用asp.net MVC时遇到以下问题:当管理员更新用户请求时,他将收到一封电子邮件,告知请求已被更新,并且在这封电子邮件中会有指向页面http://www.domain.com/request 要访问该页面,用户必须登录,这样链接将重定向到登录页面。登录后,用户被重定向到http://www.domain.com/welcome, 登录后如何重定向到电子邮件上发送的地址 [HttpPost] public ActionResult Login(LoginModel model, stri
http://www.domain.com/request
要访问该页面,用户必须登录,这样链接将重定向到登录页面。登录后,用户被重定向到http://www.domain.com/welcome
,
登录后如何重定向到电子邮件上发送的地址
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl, bool createPersistentCookie)
{
// Check if the Model is valid or not
if (ModelState.IsValid)
{
using (GNEntities entities = new GNEntities())
{
string username = model.Email;
string password = model.Password;
// Now if our password was enctypted or hashed we would have done the
// same operation on the user entered password here, But for now
// since the password is in plain text lets just authenticate directly
bool userValid = entities.Employee.Any(user => user.Email == username && user.Password == password && user.IsActive ==true);
// User found in the database
if (userValid)
{
FormsAuthentication.SetAuthCookie(username, createPersistentCookie); //false:not persistent cookie
Employee e = (Employee)entities.Employee.FirstOrDefault(user => user.Email == username);
if (e.Roles.Id==1 || e.RoleId == 3) // 3 = commercial
{
return RedirectToAction("Index", "AdminIndex", new { Area = "AdminArea" });
}
else if (e.Roles.Id == 2 || e.Roles.Id ==4)
{
int countryId = e.CountryId;
return RedirectToAction("Index", "Calendar", new { year = DateTime.Now.Year, countryId, Area = "EmpArea" });//with paramenters RedirectToAction("Index", new { id = currentcoupon.Companyid.id, Area="Admin" });
}
}
else
{
ModelState.AddModelError("", "The email or password provided is incorrect or your account has been disabled.");
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
我试过这个,但不起作用:
if (returnUrl == null)
{
int countryId = e.CountryId;
return RedirectToAction("Index", "Calendar", new { year = DateTime.Now.Year, countryId, Area = "EmpArea" });
}
else
{
return RedirectToAction(returnUrl, new {Area = "EmpArea"});
}
结果仍然不好:
这是登录域.net/login/login?ReturnUrl=%2EmpArea%2Timesheet之前的url
登录后,它生成这个urldomain.net/EmpArea/login/EmpArea/Timesheet
它会保留登录区域,我不知道为什么。在您的登录代码中,在成功验证凭据后,您会将用户重定向到
AdminIndex
或Calendar
。请注意,Login
操作有一个可以使用的returnUrl
参数
因此,在您成功登录后,请检查参数是否为空,以及该URL的用户:
if(!string.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
也许可以显示您的登录代码?在您的登录代码中,在登录后,您可以将用户重定向到AdminIndex或Calendar。尝试改用returnUrl参数。您好,David,我添加了RedirectToAction(returnUrl,new={Area=“EmpArea”}),但仍然有错误。这是登录域之前的url。net/login/login?ReturnUrl=%2EmpArea%2Timesheet,登录后它会生成此url域。net/EmpArea/login/EmpArea/TimesheetreturnUrl不是一个操作名称,它是一个完整的url,请改用
重定向(ReturnUrl)
。非常感谢