首页 > 解决方案 > 如何在 .Net Core 中禁用 cookie 密钥加密?

问题描述

我有两个应用程序(在同一个域中):第一个在 Nodejs(快速会话)上创建,第二个在 .Net Core 上创建。登录提供程序是 1 个应用程序。

2 应用程序 - 获取身份验证 cookie,它必须启动会话。会话缓存被保存到 Mongodb 中。现在 2 应用程序运行良好。但它总是使用加密密钥重新创建 cookie。在 Mongodb 中保存的会话密钥未加密。

问题出在哪里?当用户想要返回 1 个应用程序时,他会看到登录页面。因为 1 个应用程序无法识别加密的 cookie 密钥。

我认为最好的方法 - 在 2 应用程序中禁用 cookie 密钥加密。

在我看来(对不起,我是初学者)加密工作 services.AddDataProtection()。

public class AppDistributedSessionStore : ISessionStore
    {
        DistributedSessionStore innerStore;
        IStartSession startSession;

        public AppDistributedSessionStore(IDistributedCache cache,
        ILoggerFactory loggerFactory, IStartSession startSession)
        {
            innerStore = new DistributedSessionStore(cache, loggerFactory);
            this.startSession = startSession;
        }

        public ISession Create(string sessionKey, TimeSpan idleTimeout, TimeSpan ioTimeout, Func<bool> tryEstablishSession, bool isNewSessionKey)
        {
           // temp
            sessionKey = "Sfdfd_fddddufd45454dfdfdfre.WFCpMxXnpfdfdfdf7878dfdf7d8fd";
            ISession session = innerStore.Create(sessionKey, idleTimeout, ioTimeout,
            tryEstablishSession, isNewSessionKey);
            if (isNewSessionKey)
            {
                startSession.StartSession(session);
            }
            return session;
        }
    }

启动.cs

services.AddSingleton<IStartSession, InitSession>();
services.AddSingleton<ISessionStore, AppDistributedSessionStore>();

services.AddSession(o =>
            {
                o.Cookie.Name = "app_session";
                o.Cookie.Path = "/";
                o.Cookie.IsEssential = true;               
            });
services.AddMongoDbCache(options =>
            {
                options.ConnectionString = Configuration.GetSection("NoSqlSettings")["ConnectionString"];

                options.DatabaseName = "appdb";
                options.CollectionName = "sessions";
                options.ExpiredScanInterval = TimeSpan.FromMinutes(10);

            });

2 app always put new cookie with encrypted key, like: CfDJ8LDy%2F5e6ELtApkua%2Bim6shYlqsO8GwVMf%2FKtOGSJzr6tCS8c%2BvIMG%2FopD%2BWrclJin07WBDSwRp66TPeDTCFI4Mu%2BWn2RsYY06JsvkQAqRIVXc0TZBIGOKGzqD1KouKLpv%2F9m63z2tUsA2plTddJj6j6oewul4z%2FnrL4GUFJ1XM%2BWW.

如何停止加密密钥?我想查看带有密钥的 cookie:“Sfdfd_fddddufd45454dfdfdfre.WFCpMxXnpfdfdfdf7878dfdf7d8fd”

标签: asp.net-core.net-core

解决方案


推荐阅读