如何强制控制器操作只匿名访问?

编程入门 行业动态 更新时间:2024-10-25 14:33:03
本文介绍了如何强制控制器操作只匿名访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以使用 [使用AllowAnonymous] 属性,以允许用户访问一个控制器动作,但有只允许匿名用户的操作属性?例如 [AllowAnonymousOnly]

I can use the [AllowAnonymous] attribute to permit a user to access a controller action, but is there an attribute to permit only anonymous users to an action? e.g. [AllowAnonymousOnly]

推荐答案

没有。它不存在

不过,您可以通过创建自己的属性从的 AuthorizeAttribute 。

However, you can create it by creating your own attribute inheriting from the AuthorizeAttribute.

下面是一个例子。

此致看起来像:

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 } }

更多推荐

如何强制控制器操作只匿名访问?

本文发布于:2023-11-14 00:57:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!