AspNet Core CookieAuthentication with injected SessionStore

TGlatzer

During migration of an ASPNetCore 1.1 Project to ASPNetCore 2.0, we stumbled upon a Problem with the Cookie-AuthN and its SessionStore.

ASP.NET Core 1 allowed us to do something like that:

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);
}

Messy, but doing its Job.

Now with ASP.NET Core 2 app.UseAuthentication() does not have a signature allowing to modify the options, and I am not able to use DI, to get a hold of the session store.

pvasek

After long search I came accross this discussion https://github.com/aspnet/Security/issues/1338 where they mentioned IPostConfigureOptions interface. I put that together and this works for me:

1) Implement interface 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) Register this implementation to the container in Startup.ConfigureServices method

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

NLog AspNet Core 5.0

分類Dev

.NET Core 2CookieAuthenticationは有効期限を無視します

分類Dev

Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

分類Dev

Http Query Parameters in UTC in AspNet Core

分類Dev

Model binding not working in aspnet core web api

分類Dev

AspNet Core3ID構成

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core DI:TryAddとAddの使用法

分類Dev

How to validate user agains policy in code in aspnet core?

分類Dev

Swashbuckle aspnet core 2.0 Swaggerconfig.cs not created

分類Dev

Using Google OAuth to secure web services in aspnet core

分類Dev

aspnet core 2.2 web app environment variables not changing in docker

分類Dev

AspNet Core - input/output JSON serialization settings at Controller Level

分類Dev

Dotnet Core Aspnet 1.1用のSimple(st)Dockerfile

分類Dev

'SessionStore' object has no attribute 'email'

分類Dev

Getting dependency injected services in ASP MVC Core outside of request pipeline in muti-tenant app

分類Dev

Net Core NLog.Web "aspnet-user-identity"は空ですか?

分類Dev

フルフレームワークを備えたASPNET Core 2

分類Dev

AspNet Core2.0を使用したGoogleJWT認証

分類Dev

ASPNET Core 2のカスタムポリシー認証(jwt auth)

分類Dev

SameSitecookie属性を明示的に設定する方法なしASPNET Core

分類Dev

カスタムロールによるAspNet Core3.1承認

分類Dev

Why can I only call one action on a controller Blazor / aspnet core 3?

分類Dev

What is equivalent .NET Core command to Aspnet_regiis.exe –ir –enable

分類Dev

Microsoft.AspNet.Identity.CoreとMicrosoft.AspNetCore.Identityの違いは何ですか?

分類Dev

既存のMicrosoft.AspNet.IdentityDB(EF 6)をMicrosoft.AspNetCore.Identity(EF Core)に移行します

分類Dev

ASPNet Core:サービス中の機能で[承認]を使用する

Related 関連記事

  1. 1

    NLog AspNet Core 5.0

  2. 2

    .NET Core 2CookieAuthenticationは有効期限を無視します

  3. 3

    Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

  4. 4

    Http Query Parameters in UTC in AspNet Core

  5. 5

    Model binding not working in aspnet core web api

  6. 6

    AspNet Core3ID構成

  7. 7

    AspNet Core using in memory repo for data protection when running in IIS

  8. 8

    AspNet Core using in memory repo for data protection when running in IIS

  9. 9

    AspNet Core using in memory repo for data protection when running in IIS

  10. 10

    AspNet Core DI:TryAddとAddの使用法

  11. 11

    How to validate user agains policy in code in aspnet core?

  12. 12

    Swashbuckle aspnet core 2.0 Swaggerconfig.cs not created

  13. 13

    Using Google OAuth to secure web services in aspnet core

  14. 14

    aspnet core 2.2 web app environment variables not changing in docker

  15. 15

    AspNet Core - input/output JSON serialization settings at Controller Level

  16. 16

    Dotnet Core Aspnet 1.1用のSimple(st)Dockerfile

  17. 17

    'SessionStore' object has no attribute 'email'

  18. 18

    Getting dependency injected services in ASP MVC Core outside of request pipeline in muti-tenant app

  19. 19

    Net Core NLog.Web "aspnet-user-identity"は空ですか?

  20. 20

    フルフレームワークを備えたASPNET Core 2

  21. 21

    AspNet Core2.0を使用したGoogleJWT認証

  22. 22

    ASPNET Core 2のカスタムポリシー認証(jwt auth)

  23. 23

    SameSitecookie属性を明示的に設定する方法なしASPNET Core

  24. 24

    カスタムロールによるAspNet Core3.1承認

  25. 25

    Why can I only call one action on a controller Blazor / aspnet core 3?

  26. 26

    What is equivalent .NET Core command to Aspnet_regiis.exe –ir –enable

  27. 27

    Microsoft.AspNet.Identity.CoreとMicrosoft.AspNetCore.Identityの違いは何ですか?

  28. 28

    既存のMicrosoft.AspNet.IdentityDB(EF 6)をMicrosoft.AspNetCore.Identity(EF Core)に移行します

  29. 29

    ASPNet Core:サービス中の機能で[承認]を使用する

ホットタグ

アーカイブ