我知道必须对用户进行授权时有一个属性。您也可以放置[AllowAnonymous]
在其上方。另请参见下面的代码:
[Authorize] // only when the user is authorize
public class AccountController : Controller
{
[HttpGet]
[AllowAnonymous] // also when the user in not authorize.
public ActionResult Login(string returnUrl = "")
{ /* Some code */ }
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{ /* Some code */ }
}
但是还有一个属性仅允许匿名。例如:登录页面仅在用户未授权时显示?
我认为没有现有的,但是没有理由不能自己推出。但是,我要指出的是,您会付出额外的努力将内容限制为经过身份验证的用户,这似乎很奇怪:
public class AnonymousOnly : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// Do what you want to do here, e.g. show a 404 or redirect
}
}
}
然后只需使用以下新属性装饰您的类/方法:
[AnonymousOnly]
public ActionResult Login(string returnUrl = "")
{
// code
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句