DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie
这是这两天ASP.NET Core迁移中遇到的一个问题。2个ASP.NET Core站点(对应于2个不同的ASP.NET Core Web应用程序),2个站点都可以登录,但在其中任1个站点登录后,在当前站点处于登录状态,访问另外1个站点却处于未登录状态。
开始以为是CookieAuthenticationOptions的设置不一致引起的,检查代码后确认AuthenticationScheme,CookieName,CookieDomain都是一样的。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = ".AspNetCore.Cookies",
CookieDomain = ".cnblogs.com"
});
(AuthenticationScheme的默认值是"Cookies")
之后怀疑是DataProtection的密钥不一致引起的,我们用的是同一个阿里云redis实例存储密钥,存储方式上不会造成不一致。
if (Environment.IsDevelopment())
{
services.AddDistributedMemoryCache();
}
else
{
services.AddDistributedServiceStackRedisCache(options =>
{
Configuration.GetSection("redis").Bind(options);
//Workaround for deadlock when resolving host name
IPAddress ip;
if (!IPAddress.TryParse(options.Host, out ip))
{
options.Host = Dns.GetHostAddressesAsync(options.Host)
.Result.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork).ToString();
}
});
}
services.AddDataProtection().PersistKeysToDistributedStore();
为了进一步确认密钥是否是一样的,修改了 DataProtection.DistributedStore 的源代码将密钥打印在控制台,运行后确认2个站点用的密钥是一样的。
public IReadOnlyCollection<XElement> GetAllElements()
{
var data = _cache.GetString(_key);
Console.WriteLine(data);
if (!string.IsNullOrEmpty(data))
{
return XDocument.Parse(data).Root.Elements().ToList().AsReadOnly();
}
else
{
return new List<XElement>().AsReadOnly();
}
}
后来突然想到 services.AddDataProtection() 是不是有什么配置选项?F12之后发现果然有个DataProtectionOptions:
public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action<DataProtectionOptions> setupAction);
继续F12发现DataProtectionOptions只有1个属性ApplicationDiscriminator,点开它的注释后,问题的答案跃然而出:
//
// Summary:
// Provides global options for the Data Protection system.
public class DataProtectionOptions
{
public DataProtectionOptions(); //
// Summary:
// An identifier that uniquely discriminates this application from all other applications
// on the machine. The discriminator value is implicitly included in all protected
// payloads generated by the data protection system to isolate multiple logical
// applications that all happen to be using the same key material.
//
// Remarks:
// If two different applications need to share protected payloads, they should ensure
// that this property is set to the same value across both applications.
public string ApplicationDiscriminator { get; set; }
}
原来不同的ASP.NET Core应用程序要使用同样的加解密方式,除了共享密钥,还要设置同样的ApplicationDiscriminator。
添加如下的代码后问题立马解决。
services.AddDataProtection(options => options.ApplicationDiscriminator = "cnblogs.com");
DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie的更多相关文章
- ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 设置和初始化数据库 上一章节中我们已经设置和配置好了 EF ...
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...
- ASP.NET Core WebApi中使用FluentValidation验证数据模型
原文链接:Common features in ASP.NET Core 2.1 WebApi: Validation 作者:Anthony Giretti 译者:Lamond Lu 介绍 验证用户输 ...
- Cenos7 部署asp.net core站点
系统版本 rpm -q centos-release --- centos-release--5.1804.el7.centos.x86_64 安装libicu yum install libunwi ...
- .NET跨平台之旅:在生产环境中上线第一个运行于Linux上的ASP.NET Core站点
2016年7月10日,我们在生产环境中上线了第一个运行于Linux上的ASP.NET Core站点,这是一个简单的提供后端服务的ASP.NET Core Web API站点. 项目是在Windows上 ...
- .NET跨平台之旅:将QPS 100左右的ASP.NET Core站点部署到Linux服务器上
今天下午我们将生产环境中一个单台服务器 QPS(每秒请求数)在100左右的 ASP.NET Core 站点部署到了 Linux 服务器上,这是我们解决了在 .NET Core 上使用 EnyimMem ...
- .NET跨平台之旅:生产环境中第2个跑在Linux上的ASP.NET Core站点
今天我们在生产环境中上线了第2个跑在Linux上的ASP.NET Core站点.这是一个简单的Web API站点,通过命令行的方式调用安装在Linux服务器上的程序完成操作.之前用的是nodejs,现 ...
- .NET跨平台之旅:在Linux上以本地机器码(native)运行ASP.NET Core站点
在将“.NET跨平台之旅”示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 (博文链接)之后,我们有一个难以抗拒的冲动 —— 体验 ...
- 以self-contained方式在Linux上部署ASP.NET Core站点
今天准备将一个在Windows上用VS2015开发的ASP.NET Core程序部署到阿里云Linux服务器上,部署时发现这台服务器是内网服务器,无法直接安装.NET Core SDK,于是想到尝试用 ...
随机推荐
- UICollectionView在初始化的时候移动到某个距离
#pragma mark -- 使用场景:选中非第一张图片用CollectionView进行浏览时,CollectionView滑动到对应的位置 #pragma mark -- 重点在于UICol ...
- 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息
WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...
- appium 获取android 粘贴板上的内容
appium 新版本增加了获取粘贴板的内容.如果使用appium旧版本,获取粘贴板的内容不是那么容易的,甚至百度谷歌各种搜,都无法找到合适的解决方法.新版本获取android 粘贴板内容就显得很容易了 ...
- MySql实现sequence功能的代码
使用函数创建自增序列管理表(批量使用自增表,设置初始值,自增幅度) 第一步:创建Sequence管理表 sequence DROP TABLE IF EXISTS sequence; CREATE T ...
- NodeJS + PhantomJS 抓取页面信息以及截图
利用PhantomJS做网页截图经济适用,但其API较少,做其他功能就比较吃力了.例如,其自带的Web Server Mongoose最高只能同时支持10个请求,指望他能独立成为一个服务是不怎么实际的 ...
- 关于Unity中ARPG游戏人物移动(专题十一)
ARPG:动作型角色扮演类游戏 大多数的ARPG游戏都是使用摇杆操作,以第三人称摄像机的方式来跟随主角,实际上人物只走八个方向,上,下,左,右,左上,左下,右下,右上 控制角色移动的思路1: 在ARP ...
- 嵌入式开发之hi3519---PCIE DMA
http://blog.csdn.net/abcamus/article/details/76167747 大话pcie dma http://blog.csdn.net/qingfengtsing/ ...
- 面试Spring之bean的生命周期
找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...
- linux-centos下持续集成工具jenkins部署使用教程
centos下准备工作: 1.安装jdk环境 2.安装maven环境(用于构建项目) 3.git客户端(用于拉取源码) 此外码云还要准备一份java源码(springboot项目) 一.安装jenki ...
- phpstrom2018
http://www.oyksoft.com/soft/40722.html?pc=1