ASP.NET MVC授权属性,推出一个模式? [英] ASP.NET MVC Authorize Attribute to launch a modal?

查看:127
本文介绍了ASP.NET MVC授权属性,推出一个模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个网站,使使用jQuery的模式对话框做各种事情像登录和这样的。

I am working on a site that makes use of jquery modal dialogs to do various things like logging in and such.

不过,我们有一个轻微的问题与使用这些..这是我们使用了很多的我们的行动方法[授权]属性,因此正在发生的事情是,如果用户没有登录,命中,他们需要一个路线被授权,它为喜欢它的登录页面是假设,但显然这是假设是一个模式的。

However; we have one slight issue with the use of these.. which is we are using the [Authorize] attribute on a lot of our action methods and so what is happening is if the user is not logged in and hits a route that they need to be authorized for it shows the login page like it is suppose to but obviously this is suppose to be a modal.

总之长话短说,有没有一种方法来创建能够触发模式而不是拼成的登录模式实际的视图的自定义属性的授权?

Anyhow long story short, is there a way to create a custom authorize attribute that can trigger the modal instead of the actual view that makes up the login modal?

推荐答案

在这种情况下,你可以使用打开一个弹出,如果用户没有授权的自定义操作筛选器属性。结果
在这次行动中过滤只是检查,如果用户登录和一个布尔值添加到收藏ViewData的。结果
Aplly在控制器的动作特性。结果
然后在母版页添加打开弹出code的条件呈现。

In this case you can use a custom action filter attribute that opens a popup if the user is not authorized.
In this action filter just check if user is logged in and add a boolean value to the ViewData collection.
Aplly the attribute on the controller's action.
Then in the master page add conditional rendering of code that opens the popup.

在code的属性:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class PopupAuthorizeAttribute : AuthorizeAttribute
{
    private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
    {
        validationStatus = this.OnCacheAuthorization(new HttpContextWrapper(context));
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        bool isAuthorized = false;
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        if (this.AuthorizeCore(filterContext.HttpContext))
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetProxyMaxAge(new TimeSpan(0L));
            cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), null);
            isAuthorized = true;
        }

        filterContext.Controller.ViewData["OpenAuthorizationPopup"] = !isAuthorized;
    }
}

在母版页或其他普通视图中添加条件呈现:

In the master page or other common view add conditional rendering:

<% if((bool)(ViewData["OpenAuthorizationPopup"] ?? true)) { %>
 ...Your code to open the popup here...
<% } %>

这篇关于ASP.NET MVC授权属性,推出一个模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆