Asp.net mvc ASP MVC:在实现ASP MVC安全性时感到困惑

Asp.net mvc ASP MVC:在实现ASP MVC安全性时感到困惑,asp.net-mvc,security,asp.net-mvc-3,Asp.net Mvc,Security,Asp.net Mvc 3,我正在实现ASP MVC应用程序的安全部分,我对如何实现自定义成员资格提供程序感到困惑,因为似乎有很多功能我不会使用 我移植的应用程序通常通过一个名为“SecurityManager”的会话中存储的对象来管理安全性,该会话包含当前用户和表单权限集合。此集合的每个项都包含登录用户的每个表单的权限和该表单的每个字段的权限(如果假定不完全控制表单,则需要这些权限) 但是,当我看到MembershipProvider和AuthorizeAttribute标记的方法时,他们认为我将使用我的应用程序不使用的

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

我移植的应用程序通常通过一个名为“SecurityManager”的会话中存储的对象来管理安全性,该会话包含当前用户和表单权限集合。此集合的每个项都包含登录用户的每个表单的权限和该表单的每个字段的权限(如果假定不完全控制表单,则需要这些权限)

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

因此,基本上,我唯一需要的是,当发出请求时,检查安全管理器是否存储在会话中(如果不是,则用户未经过身份验证,将重定向到登录页面)然后从会话中获取该对象并对其执行操作,以了解用户是否可以访问该视图

最好的方法是什么?我已经读到绕过自定义成员资格不是一个好主意。

更新:我最近遇到了这个问题,并想出了如何使用
authorized属性来完全按照您的要求执行操作。验证用户是否为管理员的“我的”属性的工作方式如下:

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);
        }
    }
}
现在,当调用一个控制器或操作,并用
[MyCustomAuthorize]
修饰时,它将运行此代码以确定用户是否根据您的自定义逻辑进行了授权,如果没有,它将准确地重定向它们
[Authorize]
属性的工作方式


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

如果这偏离了轨道,请原谅。如果我创建从ActionFilterAttribute继承的自己的ChkSecurityAttribute并在重写的OnActionExecuting事件中检查安全性,有什么区别?从AuthorizeAttribute继承它有什么好处(它是否需要实现表单身份验证和/或成员身份)?最后,我想知道当用户未经授权时,重定向到“您无权访问”页面的最佳位置是什么。谢谢。我真的不知道,除了少做些体力活。例如,如果您创建了自己的操作过滤器,那么您必须将属性放入自己的
Roles
属性中,或者放入您希望使用的任何其他AuthorizeAttribute属性中(无可否认,这并不难)。要处理授权问题(用户已登录但没有访问权限),您需要返回403 http结果,尽管我不确定如何从authorized属性执行此操作,而且通过自定义操作筛选器可能会更容易,因此在需要时,我不怕在Asp.NET成员资格框架之外进行授权。成员资格提供程序系统主要是允许使用通用接口进行身份验证/授权,但通过使用自定义提供程序,您已经表明您的需求超出了默认提供程序的范围,在这一点上,你换成另一个非定制提供商的可能性很小。谢谢你提供的详细信息。是的,从过去开始,我就不喜欢成员API,所以我一直在寻找一些单独的东西,如果一切都可以在这个自定义属性的操作过滤器中手动处理(我可以将角色作为字符串数组)——我必须弄清楚一个简单的重定向到“No access”页面是否有效。当用户登录时,我的身份验证/授权在会话变量中设置(这里也没有成员资格)。我很乐意使用“AuthorizeAttribute”,但我是否可以在没有成员资格的情况下完成这项工作,或者这是否需要自定义成员资格提供程序(和角色提供程序)实现。我看不出您在成员资格API之外无法完成这项工作的任何原因,我也看不出仅仅创建自己的操作筛选器会更困难的原因。
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();
    }
}