【问题标题】:Can I use a ViewBag value to determine useable functions?我可以使用 ViewBag 值来确定可用功能吗?
【发布时间】:2013-05-02 03:44:47
【问题描述】:

所以我目前正在使用登录中设置的 ViewBag 来确定他们是否只能看到管理员的东西。这样做是因为 Roles.CreateRole、Membership.CreateUser 和 Roles.AddUserToRole 被禁用,因为我们使用 ModelFirst ASP.net。

public ActionResult Login(LoginModel model, string returnUrl)
    {
        ViewBag.Admin = false;
            if (model.IsValid(model.UserName, model.Password))
            {
                ViewBag.Admin = (bool)model.currentLoggedInEmployee.IsAdmin;
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
                return View(model);
            }
    }

然后我们简单地使用:

   @if (ViewBag.Admin == true) {
        <li>@Html.ActionLink("Administration", "Index", "Administration")</li>
   }

仅向管理员显示这些按钮。这行得通。

现在我们想要的是确保只有管理员才能运行某些功能,方法类似于正常

[Authenticate(Roles="Admin")]
[HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        // TODO: Add insert logic here
    }

但是因为我们没有任何“角色”,所以我们不能这样做。我们需要使用 ViewBag.Admin 值来授权人们使用这些功能。问题是,如何做到这一点?

【问题讨论】:

  • 你是如何处理身份验证的?

标签: asp.net-mvc razor roles viewbag authorize


【解决方案1】:

我建议您使用自己的AuthorizeAttribute,然后您可以从那里确定当前登录的用户是否是管理员。

当您创建身份验证 cookie 时,添加一些附加信息(即管理标志),例如

public ActionResult Login(LoginModel model, string returnUrl)
{
    if (model.IsValid(model.UserName, model.Password))
    {
         var ticket = new FormsAuthenticationTicket(1,
             model.UserName,
             DateTime.Now,
             DateTime.Now.AddMinutes(30),
             model.RememberMe,
             model.currentLoggedInEmployee.IsAdmin, // user data
             FormsAuthentication.FormsCookiePath);

         // Encrypt the ticket.
         string encTicket = FormsAuthentication.Encrypt(ticket);

         // Create the cookie.
         Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

         // Redirect back to original URL.
         return RedirectToAction("Index", "Home");
    }
    else
    {
        ModelState.AddModelError("", "Login data is incorrect!");
        return View(model);
    }
}

创建一个自定义授权属性以根据角色验证登录用户,例如

public class AdminOnlyAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext.Current.User.Identity.IsAuthenticated)
        {
            var ticket = ((FormsIdentity)User.Identity).Ticket;
            return (bool)ticket.UserData;
        }
        else
        {
             return false;
        }
    }
}

然后将你的动作装饰为:

[AdminOnly]
[HttpPost]
public ActionResult Create(FormCollection collection)
{
    // TODO: add insert logic here
}

【讨论】:

  • @user2107630 不用担心,如果用户权限在会话期间被撤销,他们仍然可以访问管理操作。最安全的选择是以您拥有最新权限的方式从数据库中重新拉取用户。
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 2018-07-08
  • 2019-08-06
  • 2016-06-13
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多