在将ASPNetCore 1.1项目迁移到ASPNetCore 2.0期间,我们偶然发现了Cookie-AuthN及其SessionStore
的问题。
ASP.NET Core 1 允许我们做类似的事情:
public void ConfigureServices(...) {
Services.AddDistributedSqlServerCache(...);
Services.AddSingleton<DistributedCookieSessionStore>(); /// SQL based store
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) {
var cookieOptions = app.ApplicationServices.GetRequiredService<IOptions<CookieAuthenticationOptions>>().Value;
cookieOptions.SessionStore = app.ApplicationServices.GetRequiredService<DistributedCookieSessionStore>();
app.UseCookieAuthentication(cookieOptions);
}
凌乱,但正在做它的工作。
现在使用 ASP.NET Core 2 app.UseAuthentication()
没有允许修改选项的签名,而且我无法使用DI来获取会话存储
答案 0 :(得分:4)
经过长时间的搜索后,我来到了这个讨论https://github.com/aspnet/Security/issues/1338,他们提到了IPostConfigureOptions
界面。我把它放在一起,这对我有用:
1)实施界面IPostConfigureOptions<CookieAuthenticationOptions>
public class PostConfigureCookieAuthenticationOptions : IPostConfigureOptions<CookieAuthenticationOptions>
{
private readonly ITicketStore _ticketStore;
public PostConfigureCookieAuthenticationOptions(ITicketStore ticketStore)
{
_ticketStore = ticketStore;
}
public void PostConfigure(string name, CookieAuthenticationOptions options)
{
options.SessionStore = _ticketStore;
}
}
2)在Startup.ConfigureServices
方法
services.AddSingleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>();