相关疑难解决方法(0)

当用户不在授权角色中时,如何提供未授权页面?

我正在使用这样的Authorize属性:

[Authorize (Roles="Admin, User")]
Public ActionResult Index(int id)
{
    // blah
}
Run Code Online (Sandbox Code Playgroud)

当用户不在指定的角色时,我会收到一个错误页面(找不到资源).所以我也将HandleError属性放入其中.

[Authorize (Roles="Admin, User"), HandleError]
Public ActionResult Index(int id)
{
    // blah
}
Run Code Online (Sandbox Code Playgroud)

现在,如果用户不在指定的角色,它将进入" 登录"页面.

当用户不符合所需角色之一时,如何让它转到未授权页面而不是登录页面?如果发生不同的错误,我如何将该错误与未授权错误区分开来并以不同方式处理?

c# security asp.net-mvc roles

41
推荐指数
2
解决办法
3万
查看次数

在MVC中设置403错误页面

我重写该类以执行自定义授权

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAuthenticated)
        {
            filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在web.config中我配置了403错误页面

<customErrors defaultRedirect="/Shared/Error" mode="On">
  <error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

但是浏览器仍然显示403的默认错误页面,我在这里缺少任何想法

unauthorized http-status-code-403 c#-4.0 asp.net-mvc-3

16
推荐指数
3
解决办法
2万
查看次数