我正在尝试在我的登录操作结果中创建不同的重定向,即如果用户登录并且角色重定向到特定视图
E.g。如果接待员登录重定向到“RedirectToAction”(“索引”,“预订”);
我尝试了一种逻辑,但它不起作用......下面就是我所做的。
private ActionResult RedirectToLocal(string returnUrl)
{
if (User.IsInRole("Doctor"))
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Patient");
}
}
if (User.IsInRole("Receptionist"))
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("PatientBooking", "BooKing");
}
}
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("GeneralSearch", "Search");
}
}
修改 下面是我在Login ActionResult
中调用方法的方法public async Task<ActionResult> Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var loginbusiness = new LoginBusiness();
var result = await loginbusiness.LogUserIn(model, AuthenticationManager);
if (result)
{
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
return View(model);
}
但是这对我来说没有任何想法???
答案 0 :(得分:0)
试试这个
private ActionResult RedirectToLocal(string returnUrl)
{
if (User.IsInRole("Doctor"))
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Patient");
}
}
else if (User.IsInRole("Receptionist"))
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("PatientBooking", "BooKing");
}
}
else
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("GeneralSearch", "Search");
}
}
}
答案 1 :(得分:0)
经过深入研究后,我得到了一个工作正常的逻辑......
var context = new DataContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
if (result)
{
var user = userManager.FindByName(model.UserName);
if (userManager.IsInRole(user.Id, "Receptionist"))
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("PatientBooking", "BooKing");
}
}
}
这有效!!!