如何在OpenIdConnectEvents.OnAuthorizationCodeReceived中访问用户的HttpContext?

时间:2019-04-02 17:24:52

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

在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显然不是我想要的。

1 个答案:

答案 0 :(得分:0)

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

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