我在操作方法上使用了Authorize属性,是否需要检查用户是否已通过身份验证?

时间:2019-11-28 12:12:56

标签: asp.net asp.net-core .net-core authorization asp.net-identity

下面的代码是我在Asp.Net Core 3中的操作方法。

[HttpGet]
[Authorize]
public async Task<IActionResult> Info()
{
    if (!User.Identity.IsAuthenticated) /// Is this need?
    {
         return BadRequest("Un Authorized Access");
    }
 }

我在方法上使用了Authorize属性。是否需要使用User.Identity.IsAuthenticated再次检查用户的身份验证?

1 个答案:

答案 0 :(得分:1)

您应该查看“身份验证与授权”之间的区别,请参见https://stackoverflow.com/a/6556548/2219991

您发布的代码是不够的,因为即使用户未通过身份验证,有时也会满足授权要求。


我的回答是错误的,请查看Joe的评论,并指出https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Http/AuthorizeAttribute.csif(ariaExpanded.equalsIgnoreCase("false")) { xpathToLi.findElement(By.xpath("./span[@role='presentation']")).click(); }属性还会检查身份验证