asp.net-core - 如何在 .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”
解决方案
推荐阅读
- javascript - 如何将占位符把手与#compare 进行比较
- c# - 为 2D 网格创建文件结构
- javascript - If语句不改变变量Javascript的值
- java - Java 读取 Windows 键状态
- php - php- 允许特殊字符 Â Ê Ç
- c - 在 Mac 中使用 C 的全局变量和动态库
- excel - 结合 VLOOKUP 和 HLOOKUP?
- python - 如何使用 Python urllib.request 进行 Web Scraping 2018
- ios - 带有特殊字符的 Swift 正则表达式
- mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“where 子句”中的未知列“C3714552”