asp.net mvc未经授权的回复是空白页吗?

8 asp.net-mvc authorization

如果我没有获得控制器操作的授权,我会得到一个空白页面而没有错误消息?我想显示某种消息,这是我的设置:

class MyAuth : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (!httpContext.User.Identity.IsAuthenticated)
            return false;

        return MyIsCurrentUserInRoles(Roles.Split(",".ToCharArray()));
    }
}
Run Code Online (Sandbox Code Playgroud)

用作

[Myauth(Roles="admin")]
class MyController: Controller
{
}
Run Code Online (Sandbox Code Playgroud)

当我没有被授权时,结果是空白页?

这是默认行为吗?如果是这样,我在哪里改变它以产生一个不真实的消息?

Rhy*_*nes 8

是的,这是在ASP.Net开发服务器中运行时的默认行为:

ASP.Net MVC授权操作过滤器

您可以通过编辑web.config将其重定向到页面,以包含错误401的重定向:

<customErrors defaultRedirect="ErrorPage.aspx" mode="On"> 
    <error statusCode="401" redirect="AccessDenied.aspx" />       
</customErrors>
Run Code Online (Sandbox Code Playgroud)