您可以通过编写自定义授权属性轻松实现此目的:
Public Class CustomAuthorizeAttribute
Inherits AuthorizeAttribute
Protected Overrides Sub HandleUnauthorizedRequest(filterContext As AuthorizationContext)
MyBase.HandleUnauthorizedRequest(filterContext)
filterContext.Controller.TempData("message") = String.Format("You need to be {0} to access this resource", Me.Roles)
End Sub
End Class
然后用这个自定义属性装饰控制器动作:
<CustomAuthorize(Roles := "Administrator")> _
Public Function AdminPage() As ActionResult
Return View()
End Function
在您的登录视图中的某处:
<div><%: TempData("message") %></div>