在C#中,如何将用户重定向到一个拒绝访问错误aspx页面? [英] In C#, how to redirect users to an access denied error aspx page?

查看:160
本文介绍了在C#中,如何将用户重定向到一个拒绝访问错误aspx页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#的Web应用程序,我指定了一些安全,只允许3个用户进入该网站:

I have a web application on C#, I have specified some security to allow only 3 users to enter the site:

<authentication mode="Windows"/>
<authorization>
    <allow users="DOMAIN\john,DOMAIN\mary,DOMAIN\jesus" />
    <deny users="*" />
</authorization>

似乎许可工作,因为他们可以进入,但人与其他登录不能进入(这是确定)。

It seems that the permission works since they can enter but people with other logons cannot enter (this is ok).

现在的问题是如何捕捉到的 401.2错误,而不触及IIS,拦截访问被拒绝错误,将用户重定向到一个用户友好的aspx页面。

The problem now is how to catch the 401.2 error, without touching IIS, to intercept the Access is denied error and redirect the user to a user-friendly aspx page.

我尝试添加一些code上global.asasx Application_OnError,但它不能正常工作。

I tried to add some code on global.asasx Application_OnError but it doesn't work.

我怎么可以重定向用户到一个错误的子文件夹一样,当他们没有被授权 /errors/AccessDenied.aspx 页?

How can I redirect user to a errors subfolder like /errors/AccessDenied.aspx page when they are not authorized?

推荐答案

在你的web.config添加自定义错误部分像

Add custom error section in your web.config like

<customErrors mode="On" defaultRedirect="/ErrorPages/AppError.html">
    <error statusCode="401" redirect="/ErrorPages/401-2.html" />
</customErrors>

如果不锻炼,你可以尝试添加以下code在Global.asax中

IF that doesn't workout you can try adding the following code in your Global.asax

PrivateSub Global_EndRequest(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMyBase.EndRequest
If Response.StatusCode = 401 AndAlso Request.IsAuthenticated = TrueThen
Response.ClearContent()
Server.Execute("~/ErrorPages/401-2.html")
EndIf
EndSub

这篇关于在C#中,如何将用户重定向到一个拒绝访问错误aspx页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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