我使用asp.net MVC有以下问题:当管理员更新用户请求时,他将收到一封电子邮件,告诉他请求已经更新,并且在这封电子邮件中将有链接到页面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"});
}
结果仍然不好:这是登录前的url domain.net/Login/Login?ReturnUrl=%2fEmpArea%2fTimesheet
,登录后生成此url domain.net/EmpArea/Login/EmpArea/Timesheet
它保留了登录区我不知道为什么
在您的登录代码中,在成功验证凭据之后,您将用户重定向到AdminIndex
或Calendar
。注意,Login
动作有一个可以使用的returnUrl
参数。
所以在你成功登录后,检查参数是否为空,并将用户重定向到那个URL:
if(!string.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);