如何通过在Asp.net MVC2 Ajax调用处理UnauthorizedRequest [英] How to handle UnauthorizedRequest via Ajax call in Asp.net MVC2

查看:106
本文介绍了如何通过在Asp.net MVC2 Ajax调用处理UnauthorizedRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:

我在我的框架归类AuthorizeAttribute在我做的自定义授权子。

I have a sub classed AuthorizeAttribute in my framework in which I am doing custom authorization.

我在通过jQuery正常asp.net MVC视图渲染渲染阿贾克斯切换的过程。因此,在应用的每一个环节做了Ajax调用来获得数据。

I am in the process of switching from normal asp.net mvc view rendering to Ajax rendering via jQuery. Hence every link in the application does a ajax call to get the data.

为了迎合这一点,我已经转换我的大部分页面的局部视图,使每一个Ajax请求只获取需要在页面上要更新的部分。

In order to cater for this I have converted most of my pages to partial views so that each ajax request only gets the portion that needs to be updated on the page.

在普通视图渲染当一个请求是未经授权的它被重定向到web.config中所描述的登录页面。转换为阿贾克斯事以后就有点不一样,因为我不想在Ajax请求的登录页面的标记,但要在它的结构化的反应,这样我可以Ajax调用内采取相应的行动。

During the normal view rendering when a request was unauthorized it was being redirected to the logon page described in the web.config. After converting to Ajax things are a bit different as I dont want the markup for the logon page in the ajax request but want a structured response in it so that I can act accordingly inside the ajax call.

在为了做到这一点,我相信我有覆盖子归类AuthorizeAttribute类HandleUnauthorizedRequest方法和filterContext.Result设置为一个JSON结果。但这样做我怎么会得到一个未经授权的请求和全成的要求来区分,因为从Ajax调用的观点都是成功的响应;因此,将在成功处理程序进行处理。

In order to do this I believe I have to override the HandleUnauthorizedRequest method in the sub classed AuthorizeAttribute class and set the filterContext.Result to a json result. But in doing so how would I get to distinguish between a unauthorized request and a successfull request, because from the ajax calls point of view both are successful responses; hence will be handled in the success handler.

什么是处理这个问题的正确方式?

What would be the correct way to deal with this issue?

推荐答案

我刚才已经想通了,我可以在HandleUnauthorized正常请求和Ajax请求的过滤器
我在我的AuthorizeAttribute子类中重写请求方法。这样一个Ajax请求我可以创建一个JSON结果或为此事别的东西,和正常的请求仍然会显示在登录页面。在code是如下:

I have just figured it out, I can filter between normal requests and ajax requests in the HandleUnauthorized Request method that I override in my AuthorizeAttribute sub class. That way for an ajax request I can create a json result or something else for that matter, and for normal requests it would still show up the login page. the code is as follows:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            JsonResult UnauthorizedResult = new JsonResult();
            UnauthorizedResult.Data = "{ request : 'unauthorized' }";
            UnauthorizedResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            filterContext.Result = UnauthorizedResult;
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }

我依然会不标记我的问题是解决了,所以如果有人可以建议做一个更好的方式,我仍然开放的建议。

still I will not mark my question as resolved, so if someone can suggest a better way of doing it, I am still open to suggestions.

这篇关于如何通过在Asp.net MVC2 Ajax调用处理UnauthorizedRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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