【问题标题】:get current user role and redirect to specific action获取当前用户角色并重定向到特定操作
【发布时间】:2015-11-04 04:47:38
【问题描述】:

我正在尝试在我的登录操作结果中创建不同的重定向,因为如果用户登录并处于角色中,则重定向到特定视图

例如如果接待员登录重定向到`RedirectToAction("Index", "Booking");

我尝试了一个逻辑,但它不起作用...以下是我所做的。

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);
    }

但这对我没有任何想法???

【问题讨论】:

  • 你有什么错误吗?
  • 它不经过验证过程,它只是重定向到返回 RedirectToAction("GeneralSearch", "Search");方法@ElenaDBA
  • 我认为问题在于你的条件逻辑

标签: model-view-controller asp.net-mvc-validation redirecttoaction


【解决方案1】:

试试这个

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");
    }
 }
 }

【讨论】:

  • 好吧,让我现在试试这个逻辑@ElenaDBA
  • 它仍然提供同样的东西......@ElenaDBA
  • 显然用户角色既不是“医生”也不是“接待员”。您是否尝试调试以查看它是什么?它可以像字母大小写一样简单
  • 角色确实以这种格式存在,我编辑了我的原始代码以添加登录操作结果@ElenaDBA
  • 我认为它还没有对用户进行身份验证。 @ElenaDBA 导致在它返回的代码行上打断( IsAuthenticated = false )
【解决方案2】:

经过大量研究,我得到了一个运行良好的逻辑......

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");
                    }

                }
            }

这行得通!!!

【讨论】:

  • :) 我知道我一定让你头疼哈哈……这不是故意的@ElenaDBA
猜你喜欢
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2021-01-22
  • 2021-05-05
相关资源
最近更新 更多