ASP.NET MVC 3 自定义身份验证/授权

发布于 2024-12-09 06:08:06 字数 1957 浏览 0 评论 0原文

我在互联网上进行了搜索,并且发现了一些关于这个主题的好东西,但是我仍然不确定一些问题:

1)我正在使用带有自定义身份验证提供程序的表单身份验证。因此,我仍然使用 Authorize 属性和 web.config 中的部分,但基本上当 FormsAuthenticationTicket 不存在时,我会重定向到登录页面(在 web.config 中指定) .config),然后利用自定义身份验证提供程序根据数据库对用户进行身份验证,然后发出 FormsAuthenticationTicket。这是正确的吗?

2) 我应该使用自定义 Authorize 属性,还是应该将 GenericPrincipalApplication_AuthenticateRequest 注入到 HttpContext 中> global.asax 页面中的事件处理程序?或者我应该在控制器操作中使用 User.IsInRole

我只需要基于角色的授权,我认为我的身份验证方案非常好。

有什么指示/建议吗?

谢谢, Sam

编辑

因此,根据我所读到的内容,最好的选择是创建自定义 AuthorizeAttribute 并覆盖 AuthorizeCore

所以我所做的是:

public class CustomAuthorize : System.Web.Mvc.AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                var model = AdminUserViewModel.FromJsonString(((FormsIdentity)httpContext.User.Identity).Ticket.UserData);
                httpContext.User = new GenericPrincipal(HttpContext.Current.User.Identity, model.SecurityGroups.Select(x => x.Name).ToArray());
            }
            return base.AuthorizeCore(httpContext);
        }

        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            //base.HandleUnauthorizedRequest(filterContext);
            filterContext.Result = new System.Web.Mvc.RedirectResult("/Authentication/NotAuthorized", false);
        }
    }

只需注入一个新的主体/身份以及存储在 FormsAuthenticationTicket UserData 属性中的角色。然后让基地完成剩下的工作。

这看起来还可以吗?

编辑 #2

我对在 IIS7 中使用 global.asax 中的 Application_AuthenticateRequest 有点厌倦,因为集成管道,每个请求都会触发该事件、图像、CSS、 js...

这是正确的吗?

I have searched all over the internet and SO, and I have found some good stuff on this topic, but I have a few questions that I am still unsure about:

1) I am using Forms Authentication with a custom Authentication provider. So I use the Authorize attribute and the section in the web.config still, but basically when the FormsAuthenticationTicket does not exist, I redirect to a login page (specified in the web.config) which then utilizes the custom Authentication Provider to auth the user against a db and then issues the FormsAuthenticationTicket. Is this correct?

2) Should I be using a custom Authorize attribute or should I just inject a GenericPrincipal into the HttpContext from the Application_AuthenticateRequest event handler in the global.asax page? Or should I be using User.IsInRole insode of the controller actions?

I just need role based authorization, and I think my Authentication Scheme is pretty good.

Any pointers/advice?

Thanks,
Sam

Edit

So from what I have read, the best option for this is to create a custom AuthorizeAttribute and override the AuthorizeCore.

So what I have done is this:

public class CustomAuthorize : System.Web.Mvc.AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                var model = AdminUserViewModel.FromJsonString(((FormsIdentity)httpContext.User.Identity).Ticket.UserData);
                httpContext.User = new GenericPrincipal(HttpContext.Current.User.Identity, model.SecurityGroups.Select(x => x.Name).ToArray());
            }
            return base.AuthorizeCore(httpContext);
        }

        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            //base.HandleUnauthorizedRequest(filterContext);
            filterContext.Result = new System.Web.Mvc.RedirectResult("/Authentication/NotAuthorized", false);
        }
    }

Simply inject a new principal/identity with the roles that are stored in the FormsAuthenticationTicket UserData property. Then let the base do the rest.

Does this seem to be OK?

Edit #2

I am a little weary of using the Application_AuthenticateRequest in the global.asax with IIS7, because of the integrated pipeline, every request fires that event, images, css, js...

Is this correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2024-12-16 06:08:06

1)我也做同样的事情。

2)我使用 Authorize 属性和 Application_AuthenticateRequest 事件处理程序。

在 Application_AuthenticateRequest 事件处理程序中,我执行如下操作:

    string[] roles = authenticationTicket.UserData.Split(',');

    if (Context.User != null)
        Context.User = new GenericPrincipal(Context.User.Identity, roles);

在控制器或操作级别,我执行如下操作:

    [Authorize(Roles = "Admin, SuperAdmin")]

1) I do the same thing.

2) I use Authorize attribute and Application_AuthenticateRequest event handler.

In Application_AuthenticateRequest event handler I do something like this:

    string[] roles = authenticationTicket.UserData.Split(',');

    if (Context.User != null)
        Context.User = new GenericPrincipal(Context.User.Identity, roles);

And at controller or action level I do something like this:

    [Authorize(Roles = "Admin, SuperAdmin")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文