2016-11-16 35 views
5

ITicketStoreMemoryCacheを使用してAuthenticationTicketを保存できますか?Cookie認証Asp.Netコア

背景:私のWebアプリケーションは、Cookie認証を使用している:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
    LoginPath = new PathString("/Authentication/SignIn"), 
    LogoutPath = new PathString("/Authentication/SignOut"), 
    ReturnUrlParameter = "/Authentication/SignIn" 
}); 

マイウェブAPIは、アクセストークン(のOAuth2)を使用して、承認プロセスを処理します。

は時々(一部のブラウザ上の)次の例外がスローされます。

未処理の例外が発生しました:チャンククッキーが不完全です。予想される2つのチャンクのうち1つだけが見つかり、合計4021文字です。クライアントのサイズ制限を超えている可能性があります。

明らかにCookieが大きすぎます。私は多くの主張を使わないので、これは奇妙です。それらはすべてデフォルトクレーム(nameidentifier、nonce、expなど)です。私は現在ITicketStoreCookieAuthenticationOptionsSessionStoreとして実装しようとしています。 AuthenticationTicketMemoryCache(このsampleのように)に格納されます。私はこれが良いアプローチであり、MemoryCacheが有効な解決策である場合、この全体の話題には非常に新しく、わかりません。

+0

https://github.com/aspnet/Security/issues/830このgithubの問題があることもをあなたにとって有益です。 –

答えて

6

私はAuthenticationTicketを保存するためにITicketStoreMemoryCacheを使用することはできますか?

これは、ここ1年間で使用してきた実装です。

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationScheme = "App.Cookie", 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
    LoginPath = new PathString("/Authentication/SignIn"), 
    LogoutPath = new PathString("/Authentication/SignOut"), 
    ReturnUrlParameter = "/Authentication/SignIn", 
    SessionStore = new MemoryCacheStore(cache) 
}); 

MemoryCacheStoreの実装は次のようになり、そしてそれはあなたが共有the example続く:

public class MemoryCacheStore : ITicketStore 
{ 
    private const string KeyPrefix = "AuthSessionStore-; 
    private readonly IMemoryCache _cache; 

    public MemoryCacheStore(IMemoryCache cache) 
    { 
     _cache = cache; 
    } 

    public async Task<string> StoreAsync(AuthenticationTicket ticket) 
    { 
     var key = KeyPrefix + Guid.NewGuid(); 
     await RenewAsync(key, ticket); 
     return key; 
    } 

    public Task RenewAsync(string key, AuthenticationTicket ticket) 
    { 
     // https://github.com/aspnet/Caching/issues/221 
     // Set to "NeverRemove" to prevent undesired evictions from gen2 GC 
     var options = new MemoryCacheEntryOptions 
     { 
      Priority = CacheItemPriority.NeverRemove 
     }; 
     var expiresUtc = ticket.Properties.ExpiresUtc; 

     if (expiresUtc.HasValue) 
     { 
      options.SetAbsoluteExpiration(expiresUtc.Value); 
     }  

     options.SetSlidingExpiration(TimeSpan.FromMinutes(60)); 

     _cache.Set(key, ticket, options); 

     return Task.FromResult(0); 
    } 

    public Task<AuthenticationTicket> RetrieveAsync(string key) 
    { 
     AuthenticationTicket ticket; 
     _cache.TryGetValue(key, out ticket); 
     return Task.FromResult(ticket); 
    } 

    public Task RemoveAsync(string key) 
    { 
     _cache.Remove(key); 
     return Task.FromResult(0); 
    } 
} 
+0

返事をありがとう。このアプローチは、クッキーを使用するのに比べて不利な点がありますか?私が考えることができるのは、Webアプリのメモリ不足です。 – jasdefer

+0

これは実際にはCookieを使用しているため、 'UseCookieAuthentication'が使用されています。記憶が懸念される場合は、これらが期限切れになり、メモリが失効すると、それらが解放され、クリーンアップされることを考慮してください。さらに、それらは小さく...同様に、アプリケーションをホストするためのセットアップ方法に応じて、影響があるかどうかを判断できます。あなたは拡大縮小が可能であることを望みます。 –

+0

メモリーは本当に問題ではありません。私は一般的な不利益について考えていただけです。ええ、それはクッキーを使用しますが、 'AuthenticationTicket'自体はクッキーには保存されません。これは正しいです? – jasdefer

関連する問題