如何在MVC中为401创建自定义错误页面 [英] How to create custom error page for 401 in MVC

查看:70
本文介绍了如何在MVC中为401创建自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我一直在面对这个问题并尝试了几乎所有的东西,但是无法得到我想要的东西。



我想显示代码401的自定义错误页面,如果用户无权查看我的webApplication的任何特定部分。请解决我的问题。



我尝试了什么:



这个是我的web.config文件:



< customErrors mode =On>

< error statusCode =404redirect =〜/ Error / NotFound/>

< error statusCode =401redirect =〜/ Home / SecureMethod/>







这是我的控制器:



[HandleError]

公共类HomeController:控制器

{



[授权]

public ActionResult SecureMethod()

{





返回查看();

}



[AllowAnonymous]

public ActionResult NonSecureMethod()

{

return View();

}



我还启用了Windows身份验证d IIS服务器上的匿名身份验证

解决方案

如果启用了匿名身份验证,则每个未经身份验证的用户都将自动映射到在那里定义的用户,因此可能没有如果您的匿名身份验证后面的用户拥有足够的权限,则身份验证错误...

因此,如果您希望在访问之前对您网站的用户进行身份验证,请关闭匿名身份验证。

Hi. I've been facing this issue so long and tried almost everything but couldn't get what I want.

I want to display an custom error page for code 401 , if the user is not authorized to view any particular part of my webApplication. Kindly solve my problem.

What I have tried:

This is my web.config file:

<customErrors mode="On">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="401" redirect="~/Home/SecureMethod"/>



This is My Controller:

[HandleError]
public class HomeController : Controller
{

[Authorize]
public ActionResult SecureMethod()
{


return View();
}

[AllowAnonymous]
public ActionResult NonSecureMethod()
{
return View();
}

I have also enabled the Windows Authentication and Anonymous Authentication on IIS Server

解决方案

If 'Anonymous Authentication' is enabled every not-authenticated user will be mapped automatically to the user defined there, so there may be no error of authentication at all if your user behind 'Anonymous Authentication' has enough rights...
So if you want to users of your site to authenticate before getting access, turn 'Anonymous Authentication' off...


这篇关于如何在MVC中为401创建自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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