ASP MVC:实现ASP MVC安全性时感到困惑

pad*_*ike 6 security asp.net-mvc asp.net-mvc-3

我正在实现ASP MVC应用程序的安全部分,我对如何实现自定义成员资格提供程序感到困惑,因为似乎有很多我不会使用的功能.

我正在移植的应用程序通常通过存储在名为"SecurityManager"的会话中的对象来管理安全性,该对象包含当前用户和表单权限集合.此集合的每个项目都包含每个登录用户表单的权限和该表单的每个字段的权限(如果不是完全控制表单,则需要它们).

但是,当我看到MembershipProvider和AuthorizeAttribute标记的方法时,他们认为我将使用我的应用程序不使用的角色,我们只有权限组,这些权限组只是为某些用户组组合在一起的权限,但它们往往会更改及时.

所以基本上我唯一需要的就是在发出请求时会检查安全管理器是否存储在会话中(如果不是用户未经过身份验证并将被重定向到登录页面),那么就得到了来自会话的对象并使用它执行操作以了解用户是否可以访问该视图.

对此最好的方法是什么?我读过绕过自定义会员资格并不是一个好主意.

Kal*_*exx 1

更新:我最近遇到了这个问题,并弄清楚了如何使用它来AuthorizeAttribute完全按照您的意愿去做。我的属性用于验证用户是否是管理员,其工作原理如下:

public class AuthorizeAdminAttribute : AuthorizeAttribute
{
    public bool IsValidUser { get; protected set; }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) { throw new ArgumentNullException("httpContext"); }

        // Make sure Forms authentication shows the user as authenticated
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // Retrieve the unit of work from Windsor, and determine if the current user is an admin
        var unitOfWork = Bootstrapper.WindsorContainer.Resolve<IUnitOfWork>();
        var user = new UserByIdQuery(unitOfWork).WithUserId((int)Membership.GetUser().ProviderUserKey).Execute();

        if (user == null)
            return false;

        // Otherwise the logged in user is a real user in the system
        IsValidUser = true;

        return user.IsAdmin;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext == null) { throw new ArgumentNullException("filterContext"); }

        // If this user is a valid user but not an admin, redirect to the homepage
        if (IsValidUser)
        {
            // Redirect them to the homepage
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
            {
                { "area", "" },
                { "action", "Index" },
                { "controller", "Home" }
            });
        }
        else
        {
            // User isn't logged in, perform normal operations
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

本质上,您必须使用 来AuthorizeCore()确定用户是否已登录,存储该结果,然后对系统中的角色执行授权。然后,您HandleUnauthorizedRequest必须弄清楚请求是否未经授权,因为用户未登录,或者是因为他们未经授权。


旧答案Authorize我通过创建该类的子类来 完成使用该属性AuthorizeAttribute。例如:

public class MyCustomAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) { throw new ArgumentNullException("httpContext"); }

        // Make sure Forms authentication shows the user as authenticated
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // replace with whatever code you need to call to determine if the user is authorized
        return IsUserAuthorized();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当调用控制器或操作并用其装饰时,[MyCustomAuthorize]将运行此代码来确定用户是否根据您的自定义逻辑获得授权,如果没有,将按照[Authorize]属性的方式准确重定向它们。

我不知道这是否是最好的方法,但这是我想出的方法。