【问题标题】:I used Authorize attribute on Action Method ,do I need to check that User Is Authenticated or not?我在 Action Method 上使用了 Authorize 属性,我是否需要检查用户是否经过身份验证?
【发布时间】:2020-03-24 02:20:57
【问题描述】:

以下代码是我在 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 再次检查用户的身份验证吗?

【问题讨论】:

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


    【解决方案1】:

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

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


    我的答案是错误的,请查看 Joe 的评论并指出 https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Http/AuthorizeAttribute.cs [Authorize] 属性也检查身份验证

    【讨论】:

    • "...有一种情况是用户满足授权要求,即使没有经过身份验证" - 你有一个例子吗?据我从源代码中可以看出,如果User.Identity.IsAuthenticated 不是trueAuthorizeAttribute.IsAuthorized 返回 false:github.com/aspnet/AspNetWebStack/blob/master/src/…
    • 即使我试着想一个我会使用匿名用户的例子,你在身份验证之后是授权是正确的,例如基于引用 URL 的授权,我必须创建一个经过身份验证的声明或编写一个覆盖 IsAuthorized 的自定义授权提供程序 - 我会更正我的答案
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多