How do I serve up an Unauthorized page when a user is not in the Authorized Roles?

前端 未结 5 771
眼角桃花
眼角桃花 2020-12-04 15:54

I am using the Authorize attribute like this:

[Authorize (Roles=\"Admin, User\")]
Public ActionResult Index(int id)
{
    // blah
}
5条回答
  •  被撕碎了的回忆
    2020-12-04 16:18

    You could do this in two ways:

    1. Specify the error the HandleError-attribute, and give a view that should be shown:

      [HandleError(ExceptionType = typeof(UnAuthorizedException), View = "UnauthorizedError")]

    You can specify several different ExceptionTypes and views

    1. Create a custom ActionFilter, check there for credentials, and redirect to a controller if the user is unauthorized.: http://web.archive.org/web/20090322055514/http://msdn.microsoft.com/en-us/library/dd381609.aspx

提交回复
热议问题