在ASP.NET核心应用程序中,我有如下代码:
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
显然不是我想要的。
context
委托的OnAuthorizationCodeReceived
参数包含保存当前http上下文的HttpContext
属性
OnAuthorizationCodeReceived = async context =>
{
var httpContext = context.HttpContext;
//..
}