首页 > 解决方案 > 如何确保 HttpContext.Current 在 IdentityServer 通知期间不会丢失

问题描述

我有一个场景,我在设置代码中的客户端中使用通知事件非常简单,如下所示。这是一个使用 AspNetCore IdentityServer4 的 AspNet Mvc 4.72 客户端:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ClientId = "members",
    ...
    Notifications = new OpenIdConnectAuthenticationNotifications
    {

        SecurityTokenValidated = notification =>
        {
            ... 
            // Sometimes this is null but I have to make sure it is 
            // always available so I can initialize stuff here.
            HttpContext.Current.<Use context here>;
            return Task.CompletedTask;
        }
    }
});

实际上,更具体地说,我需要访问会话状态,但与其他一些直接相关的需求无关紧要。我不确定为什么会发生这种情况,但根据我的研究,它与 SynchronizationContext 和 AspNet Mvc 请求的生命周期有关。

如果有人对如何确保 HttpContext 的存在有明确的答案,请告诉我。

标签: asp.net-coresession-statehttpcontextsynchronizationcontext

解决方案


推荐阅读