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,于是想到尝试用 ...
随机推荐
- M1 卡技术规范
射频卡简单来讲就是卡的一种工作方式,通过感应的方式来工作,也能够把全部的感应卡都统称为射频卡. IC卡的范围比較广.芯片外露的接触式IC卡.芯片内置的感应式IC卡和双界面IC卡都可统称为IC卡.IC卡 ...
- MySQL空间索引简单使用
简述 MySQL在5.7之后的版本支持了空间索引,而且支持OpenGIS几何数据模型.国内的MySQL相关的书籍都比较老了,在这方面有详细描述的还没有见过.有一本比较新的PostgreSQL的数据介绍 ...
- Grails 第一课
package helloworld import org.junit.*; import java.util.*; class HelloController { def index() { //d ...
- [转载]说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
原文地址:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 由于Sencha Touch 2 ...
- 使用jackson来进行数组格式的json字符串转换成List。
有一个字符串如下.如下,也是通过jackson把list转换成的json字符串,我想把它转过来,看网上的内容都不尽人如意,都是片断的内容.估计只有写的知道怎么使用,所以就直接看了jackson的官网, ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- Android Studio 插件(plugins)或者intellij idea 插件(plugins)无法安装
通常这种情况出现都因为idea.properties修改了 idea.system.path=${指定路径}/system idea.plugins.path=${idea.config.path}/ ...
- requestNextAnimationFrame兼容主浏览器方法
window.requestNextAnimationFrame = (function () { var originalRequestAnimationFrame = undefined, wra ...
- Centos 6.4 安装erlang&rabbitmq
1. 安装 erlang 1.1 准备工作,先安装依赖库 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-d ...
- Java注解小记
java注解是jdk1.5以后新出的特性,注解提升了Java语言的表达能力,有效地实现了应用功能和底层功能的分离,框架/库的程序员可以专注于底层实现. 1.Java内置注解 主要有三个: @Overr ...