asp.net-mvc - ASP MVC : Getting confused when implementing ASP MVC Security

标签 asp.net-mvc security asp.net-mvc-3

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

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

但是,当我看到 MembershipProvider 和 AuthorizeAttribute 标记的方法时,他们假设我将使用我的应用程序不使用的角色,我们只有权限组,这些权限组只是针对某些用户组分组在一起的权限,但它们往往会随着时间而改变。

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

最好的方法是什么?我了解到绕过自定义成员(member)资格并不是一个好主意。

最佳答案

更新:我最近遇到了这个问题,并弄清楚了如何使用 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);
        }
    }
}

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

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

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();
    }
}

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

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

关于asp.net-mvc - ASP MVC : Getting confused when implementing ASP MVC Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585689/

相关文章:

c# - 将网站和wcf服务发布到azure

asp.net-mvc - 在asp.net mvc5中查询两个日期值

php - "mysqli_real_escape_string"是否足以避免 SQL 注入(inject)或其他 SQL 攻击?

security - 我应该 XSS 保护来自 API 的响应吗?

c# - 从 Controller 回发后保留 ASP MVC FormCollection 值

Django 和 Facebook : security & design for a facebook webapp that performs a third party login on behalf of the user

asp.net - 寻找 ASP.NET (MVC 3) 论坛引擎

jquery 智能感知 vs2010 mvc3

asp.net-mvc - 在循环/foreach MVC View 中动态生成表

asp.net-mvc - 来自对象的动态导航