【问题标题】:How to check user permission Role Access before executing a method in ASP.NET MVC4如何在 ASP.NET MVC4 中执行方法之前检查用户权限角色访问
【发布时间】:2012-07-19 14:08:16
【问题描述】:

我已经编辑了我的问题,这是我用于实现身份验证的代码。

继承 AuthorizeAttribute 的类。

public class FBxAuth : AuthorizeAttribute
    {

        public FBxAuth()
            : base()
        {

        }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool isAuthenticated = false;
            if (httpContext.User.Identity.IsAuthenticated)
            {
                // here I will check users exists in database.
                // if yes , isAuthenticated=true;
            }
            return isAuthenticated;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Response.Redirect("/home/Register/?returningURL=" +
                filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));

        }

    }

我的控制器

[FBxAuth]
        public ActionResult Index()
        {
            teamDA = new TeamDataAccess();
            var teams = teamDA.TeamsList();

            return View(teams);
        }
  1. 我的方法是否正确?

2.如何检查经过身份验证的用户是否有权在控制器中执行操作。 例如:删除。 www.abc.com/teams/5/delete 将执行删除 我可以从 UI 中隐藏删除链接。 但是,如果用户尝试通过提供上述 url 来删除,我该如何阻止他执行该操作?

【问题讨论】:

    标签: c# asp.net-mvc authentication authorization security


    【解决方案1】:

    您必须执行与索引操作相同的操作,只需将[FBxAuth] 或通用[Authorize] 属性添加到您希望仅允许经过身份验证的用户访问的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多