【问题标题】:How do I access the User's HttpContext in OpenIdConnectEvents.OnAuthorizationCodeReceived?如何在 OpenIdConnectEvents.OnAuthorizationCodeReceived 中访问用户的 HttpContext?
【发布时间】:2019-08-24 02:43:11
【问题描述】:

在 ASP.NET Core 应用程序中,我有如下代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options => ...)
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        ...
        options.Events = new OpenIdConnectEvents
        {
            ...
            OnAuthorizationCodeReceived = async context =>
            {
                // Here I want to access the user HttpContext. previously System.Web.HttpContext.Current
            }
        };
    });
    ...
}


}

我需要访问当前用户HttpContext 的原因是我正在处理一些在用户会话中缓存访问令牌的AuthenticationContext.AcquireTokenByAuthorizationCodeAsync 逻辑。以前版本的代码使用了System.Web.HttpContext.Current,据我了解,它已从 ASP.NET Core 中删除。 AuthorizationCodeReceivedContext.HttpContext 显然不是我要找的。​​p>

【问题讨论】:

  • 你的意思是context.Request.HttpContext
  • 或者只是context.HttpContext
  • @KirkLarkin @DavidG 这些与控制器公开的HttpContexts 相同吗? (仅供参考,我只需要能够访问同一个会话)。
  • 是的,应该是一样的。

标签: c# asp.net-core openid-connect


【解决方案1】:

OnAuthorizationCodeReceived 委托的 context 参数包含保存当前 http 上下文的 HttpContext 属性

OnAuthorizationCodeReceived = async context =>
{
    var httpContext = context.HttpContext;
    //..
}

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 2022-01-16
    • 1970-01-01
    • 2019-11-17
    • 2019-11-17
    • 2010-12-10
    • 2019-05-17
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多