c# - 如何强制仅匿名访问 Controller 操作?

标签 c# asp.net-mvc authentication asp.net-mvc-5

我可以使用 [AllowAnonymous] 属性来允许用户访问 Controller 操作,但是是否有一个属性只允许匿名用户访问某个操作?例如[AllowAnonymousOnly]

最佳答案

没有。它不存在。

但是,您可以通过创建自己的继承自 AuthorizeAttribute 的属性来创建它.

Here's an example .

你的看起来像:

public class AllowAnonymousOnlyAttribute : AuthorizeAttribute
{    
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        // make sure the user is not authenticated. If it's not, return true. Otherwise, return false
    }
}

关于c# - 如何强制仅匿名访问 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513828/

相关文章:

c# - Razor View : Minify text with three dots and show full text with tooltip

css - 如何将上边距应用到我的 Html.BeginForm

web-services - 使用 Windows 身份验证设置安全 SSL、WCF

facebook - Facebook/Twitter 是否在自己的应用程序中使用 oAuth?

c# - 在 Windows 事件跟踪 (ETW) 中,TraceEventSession 无法捕获记事本中的读取事件

c# - 在 Web api 和身份 C# 中获取无效的 token 重置密码时间?

c# - 自定义 Api 授权忽略 AllowAnonymous

java - 如何访问受密码保护的网络上的 mp4 视频

c# - 从 USB 条码设备 C# 读取数据的标准方法

C# 捕获堆栈溢出异常