登录ASP.NET MVC3

时间:2011-03-18 03:12:34

标签: asp.net-mvc

我目前正在使用MVC 3开发一个应用程序......现在我陷入了登录角色......

如何创建具有不同角色的登录网站?我的意思是当管理员登录时我想重定向到后端页面,当它是普通用户重定向到该站点时。我已经为管理员设置了ROLE,但不是用户......

    [Authorize(Roles = "user")]
    public ActionResult Index()
    {
        return View();
    }

    [Authorize(Roles = "Administrators")]
    public ActionResult view()
    {
        return View();
    }

在帐户控制器中,我需要设置重定向页面或如何设置?

1 个答案:

答案 0 :(得分:8)

在LogOn方法中,您可以根据用户名重定向到相应的视图:

[HttpPost]
public ActionResult LogOn(LogOnViewModel model)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.Username, model.Password))
        {
            FormsService.SignIn(model.Username, false);
            if (string.Equals("admin", model.Username))
            {
                // If the admin user logged in
                // redirect to a different action
                return RedirectToAction("View", "Home");
            }
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    return View(model);
}