SslStream, disable session caching

前端 未结 2 600
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 23:24

The MSDN documentation says

The Framework caches SSL sessions as they are created and attempts to reuse a cached session for a new request, if possibl

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 00:15

    Caching is handled inside SecureChannel - internal class that wraps SSPI and used by SslStream. I don't see any points inside that you can use to disable session caching for client connections.

    You can clear cache between connections using reflection:

    var sslAssembly = Assembly.GetAssembly(typeof(SslStream));
    
    var sslSessionCacheClass = sslAssembly.GetType("System.Net.Security.SslSessionsCache");
    
    var cachedCredsInfo = sslSessionCacheClass.GetField("s_CachedCreds", BindingFlags.NonPublic | BindingFlags.Static);
    var cachedCreds = (Hashtable)cachedCredsInfo.GetValue(null);
    
    cachedCreds.Clear();
    

    But it's very bad practice. Consider to fix server side.

提交回复
热议问题