获取正在签出的当前 ClaimsPrincipal 的方法是什么?

huangapple go评论62阅读模式
英文:

How do I get hold of the current ClaimsPrincipal who's being SignedOut?

问题

在我的自定义 CookieAuthenticationEvents 类型中,我已经重写了SigningOut,如下所示,但无法弄清如何获取正在注销的Principal。我试图这样做是为了记录谁/何时登录和注销应用程序。

public override Task SigningOut(CookieSigningOutContext context)
{
    // 如何获取ClaimsPrincipal?
    return base.SigningOut(context);
}

然而,SigningOut重写提供了一个类似这样的Principal属性:

public override Task SigningIn(CookieSigningInContext context)
{
    _logger.LogInformation($"Signing In SessionID: {context.Principal?.GetClientSessionId()}");
    return base.SigningIn(context);
}
英文:

In my custom CookieAuthenticationEvents type I have overriden SigningOut like this, but cannot figure out how to get hold of the Principal who's being signed out. I am trying to do this so I can log who/when is being logged in and out of the app.

    public override Task SigningOut(CookieSigningOutContext context)
    {
        // How to get hold of the ClaimsPrincipal?
        return base.SigningOut(context);
    }

The SigningOut override however does provide a Principal property like this:

    public override Task SigningIn(CookieSigningInContext context)
    {
        _logger.LogInformation($"Signing In SessionID: {context.Principal?.GetClientSessionId()}");
        return base.SigningIn(context);
    }

答案1

得分: 1

你可以使用 context.HttpContext.User

public override Task SigningOut(CookieSigningOutContext context)
{
    var user = context.HttpContext.User;

    // 记录用户信息

    return base.SigningOut(context);
}
英文:

You can use context.HttpContext.User

public override Task SigningOut(CookieSigningOutContext context)
{
    var user = context.HttpContext.User;

    // log the user

    return base.SigningOut(context);
}

huangapple
  • 本文由 发表于 2023年7月18日 02:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定