2017-08-28 108 views
2

在将ASPNetCore 1.1项目迁移到ASPNetCore 2.0期间,我们偶然发现了Cookie-AuthN及其SessionStore的问题。注入SessionStore的AspNet核心CookieAuthentication

ASP.NET核心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睿2app.UseAuthentication()没有签名允许修改的选项,我无法使用DI,获得会话存储的保持。

回答

0

经过漫长的搜索,我来到这里讨论https://github.com/aspnet/Security/issues/1338他们提到的IPostConfigureOptions接口。我把那在一起,这对我的作品:

1)实现接口IPostConfigureOptions<CookieAuthenticationOptions>

public class PostConfigureCookieAuthenticationOptions : IPostConfigureOptions<CookieAuthenticationOptions> 
{ 
    private readonly ITicketStore _tickerStore; 

    public PostConfigureCookieAuthenticationOptions(ITicketStore tickerStore) 
    { 
     _tickerStore = tickerStore; 
    } 

    public void PostConfigure(string name, CookieAuthenticationOptions options) 
    { 
     options.SessionStore = _tickerStore; 
    } 
} 

2)注册该实现的容器Startup.ConfigureServices方法

services.AddSingleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>();