MVC授权属性拒绝

时间:2010-10-25 02:11:32

标签: asp.net-mvc vb.net authorization

我正在使用Authorize()属性来保护我的控制器/操作,并且只希望向未经身份验证的用户显示“登录”操作 - 或者换句话说,拒绝对经过身份验证的用户进行访问。

我无法在网上找到任何拒绝许可或允许否定权限的内容(即!LoggedIn)

有人可以指出我正确的方向吗?

MVC2,.Net 4

编辑:为了克制,我想要这样的事情:

Public Class PublicController
    Inherits ControllerBase

    <Authorize()> 'Only logged-in users can logout
    Public Function Logout() as ActionResult
        Return View()
    End Function

    'Something here to indicate that only NON-authorized users should see this action
    Public Function Login() as ActionResult
        Return View()
    End Function

End Class

1 个答案:

答案 0 :(得分:13)

可能就这么简单:

public class DenyAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return !base.AuthorizeCore(httpContext);
    }
}