简体   繁体   English

CustomError IIS6和JQuery Ajax异常处理

[英]CustomError IIS6 and JQuery Ajax exception handling

I have an issue where there are two levels of exception handling happening. 我有一个问题,其中发生了两个级别的异常处理。

I make an ajax request using jQuery, and from the server I throw back custom error exceptions; 我使用jQuery提出了ajax请求,并从服务器上抛出了自定义错误异常。 which is basically a statuscode 500 error. 这基本上是一个状态码500错误。

This is picked up dynamically on my login page that I'm creating for things like, "user does not exist" or "password does not match". 这是在我为诸如“用户不存在”或“密码不匹配”之类的登录页面动态创建的。 The user is not redirected to a new page, the status is simply updated as a status update. 用户不会被重定向到新页面,状态只是作为状态更新而更新。 This currently works. 目前有效。

However, if the user successfully logs in, they get redirected to a page. 但是,如果用户成功登录,他们将被重定向到页面。 This new page looks at whether or not a user has assigned roles to view a page or not. 新页面将查看用户是否已分配角色来查看页面。 Since customerrors from the web.config was turned off, users would be taken to a yellow page of death; 由于来自web.config的customerrors被关闭,用户将被带到死亡的黄页。 signifying that a user doesn't not have permissions or access is denied. 表示用户没有权限或访问被拒绝。

The logical way to handle this is by redirecting a page for 403 errors and configuring that in web.config : 处理此问题的逻辑方法是重定向页面以查找403错误并在web.config配置:

<customErrors mode="RemoteOnly">    
    <error statusCode="403"   redirect="~/accessdenied.aspx"/>
</customErrors>

Now this works if users don't have sufficient roles, but by going back to the original thing with jQuery handling 500 error requests, I get empty messages and throws a javascript error that it doesn't know how to parse the error response. 现在,如果用户没有足够的角色,则可以使用此方法,但是通过使用jQuery处理500个错误请求返回到原始内容,我得到了空消息,并抛出了一个JavaScript错误,它不知道如何解析错误响应。

Here's a similar question asked on stackoverflow, but it never helped: 这是一个关于stackoverflow的类似问题,但没有帮助:

jQuery ajax in ASP.NET with customErrors mode="On" ASP.NET中具有customErrors mode =“ On”的jQuery ajax

The difference between my question and that question is that the login handles the 403 and 500 errors. 我的问题与该问题之间的区别在于,登录名处理的是403和500错误。 By my login handles 500 errors, and is taken to a new page that handles 403 errors. 通过我的登录,可以处理500个错误,并转到处理403个错误的新页面。 Adding changes to the web.config changes the entire site in error handling. 将更改添加到web.config会更改整个站点的错误处理。

So to summarize.. with customerrors mode = off, jQuery exception works, but custom 403 redirects don't. 综上所述,使用customerrors mode = off时,jQuery异常有效,但是自定义403重定向无效。 With customerrors mode = on. 使用customerrors模式=开启。 jQuery exceptions don't work, but 403 redirect does. jQuery异常无效,但403重定向有效。

Is there any ways to fix this that anyone can come up with? 有什么方法可以解决这个问题,任何人都可以提出来? One way I thought was for the login to handle 403 as well, and hope that roles matched from login will match roles in the incoming page. 我认为一种方法是使登录名也能处理403,并希望从登录名匹配的角色将与传入页面中的角色匹配。 But I'm iffy on that because the login system resides on a different system than the page its redirecting to. 但是我对此很怀疑,因为登录系统与重定向到的页面位于不同的系统上。

Well, I opted to get rid of the customerror, and just throw a custom 403 exception from the server, which javascript will intercept. 好吧,我选择摆脱customerror,只是从服务器上抛出一个自定义403异常,JavaScript将拦截该异常。 Then I redirect to an accessdenied page. 然后,我重定向到一个被拒绝访问的页面。

So this is my c# code 这是我的C#代码

if (!allowedRoles)
{
     Logout();
     throw new HttpException(403, "User does not have permissions to view this page");
}

Then in my jquery: 然后在我的jQuery中:

function onLoginError(error, textStatus) {
    var jsonErrorData = JSON.parse(error.responseText).Message;
    $('#status').text(jsonErrorData );
}

and my web.config 和我的web.config

<customErrors mode="Off"></customErrors>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM