「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。

-[[戻る>ASP.NET CoreのSession利用方法]]

* 目次 [#c145203e]
#contents

*概要 [#p414f41d]
昔で言う、[[StateServerやSQL Server>ASP.NET Session#d2e4d507]]のモード。

*詳細 [#ld66eff4]

**Redisが一般的 [#a2f8eb2f]
2.1以前のバージョンでは、AddDistributedRedisCacheを、~
2.2以降のバージョンでは、AddStackExchangeRedisCacheを使う。
**Redis [#a2f8eb2f]
Redisが一般的

***サンプル [#a91d47c9]
.NET Core 2.1以前のバージョンでは、AddDistributedRedisCacheを、~
.NET Core 2.2以降のバージョンでは、AddStackExchangeRedisCacheを使う。

-NuGet
--StackExchange.Redis
--Microsoft.Extensions.Caching.StackExchangeRedis
--Microsoft.AspNetCore.DataProtection.StackExchangeRedis

-コード
 using StackExchange.Redis;
 using Microsoft.AspNetCore.DataProtection;
 
 ...
 
 var redis = ConnectionMultiplexer
     .Connect(Environment.GetEnvironmentVariable("REDIS"));
 services
     .AddDataProtection()
     .PersistKeysToStackExchangeRedis(redis, "DataProtectionKeys");
 services.AddStackExchangeRedisCache(option =>
 {
     option.Configuration = Environment
         .GetEnvironmentVariable("REDIS");
     option.InstanceName = "RedisInstance";
 });

***config [#he29af8d]
Connectメソッドの引数はconfigで、ココに、以下のように、~
「,」区切りで、色々なオプションを設定可能であるもよう。

 ConnectionMultiplexer connection = 
        ConnectionMultiplexer.Connect("endpoint,password=password,ConnectTimeout=10000");

-参考
--Configuration | StackExchange.Redis~
https://stackexchange.github.io/StackExchange.Redis/Configuration.html#configuration-options

**[[データ保護>ASP.NET Coreのデータ保護]] [#vc64fd93]
-昔で言う、machine.config の machineKeyの話。

-[[前述のサンプル・コード>#a2f8eb2f]]のように、
--AddDataProtection()
--SetApplicationName()
--PersistKeysToStackExchangeRedis()

>を呼び出す。

*参考 [#ga7db276]

**Microsoft Docs [#cf9dc121]
-ASP.NET Core での分散キャッシュ~
https://docs.microsoft.com/ja-jp/aspnet/core/performance/caching/distributed

**Medium [#x16d84e2]
-ASP.NET Core Distributed Session with Redis | by Luiz Adolphs~
https://medium.com/@LuizAdolphs/asp-net-core-distributed-session-with-redis-8ce8bacf26cd
-How to distribute Data Protection keys with an ASP.NET Core web app | by Tiago Araújo | The Startup | Jun, 2020~
https://medium.com/swlh/how-to-distribute-data-protection-keys-with-an-asp-net-core-web-app-8b2b5d52851b

**銀の光と碧い空 [#baecc266]
-ASP.NET Core on Linux で Session Replication するにはNFSが必要そうだというお話~
http://tech.tanaka733.net/entry/aspnet-core-in-linux-sessoin-replocation
-ASP.NET Core で複数Webサーバーでセッションを共有するときは、~
IDistributedCacheとIDataProtectionに注意しないといけない話~
http://tech.tanaka733.net/entry/session-sharing-in-aspnetcore


----
Tags: [[:.NET開発]], [[:.NET Core]], [[:ASP.NET]], [[:ASP.NET MVC]]


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS