【问题标题】:Is there a way to get the Auth Cookie data on Anonymous endpoints?有没有办法在匿名端点上获取 Auth Cookie 数据?
【发布时间】:2020-08-11 00:55:38
【问题描述】:

我有这种情况,我有一个匿名端点可以自己工作,但是如果用户已经使用 cookie 进行了身份验证,我需要从该经过身份验证的 cookie 会话中获取有关经过身份验证的用户的数据,但是当我在网上搜索时我还没有找到解决方案,尽管用户拥有经过身份验证的会话 cookie,但User.Identity.IsAuthenticated 始终为假,但在匿名端点上,它就像被忽略了,所以我无法确定 cookie 中的用户数据是否。

有没有办法做到这一点?

所需行为示例:

[HttpGet,AllowAnonymous]
public async Task<IActionResult> GetSomething()
{
    if(User.Identity.IsAuthenticated){
        //Get data from User.Claims and act upon it
    } else {
        //Get data without user logic
    }
}

【问题讨论】:

    标签: c# authentication asp.net-core session-cookies asp.net-core-3.0


    【解决方案1】:

    您仍然必须使用Authorize 属性。如果用户未经授权,AllowAnonymous 属性仍然允许访问。

    [HttpGet,Authorize,AllowAnonymous]
    public async Task<IActionResult> GetSomething()
    

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2012-11-30
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      相关资源
      最近更新 更多