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,于是想到尝试用 ...
随机推荐
- [BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...
- 简单shell指令
第一部分 简单的常用指令 1.date命令 date 2.显示日历 cal 3.显示当前目录 pwd 4.切换当前工作目录 默认情况下,超级用户的主目录是/root,而普通用户的主目录是/home下 ...
- Django Web开发学习笔记(1)
一.Python的标准类型 (1)bool型 >>> bool("") False >>> bool(None) False >>& ...
- nginx(一)下载和启动
一.准备工作 http://nginx.org/en/download.html 下载,解压,配置,编译,安装,都用默认的设置即可. 二.安装完成后 默认的安装路径为 /usr/local/nginx ...
- 5 Protocols For Event-Driven API Architectures
The internet is a system of communication, and as such, the relationship between client and server, ...
- react-无状态组件
import React, { Component } from "react"; //import PostItem from "./PostItem"; / ...
- 【emWin】例程十一:GIF图像显示
介绍: 本例程介绍gif格式图像显示的方法以及在GMT70,iCore3_ADP,7寸液晶模块.4.3寸液晶模块, VGA模块上的移植. 实验指导书及代码包下载: 链接:http://pan.baid ...
- React Native - FlexBox弹性盒模型
FlexBox布局 1. 什么是FlexBox布局? 弹性盒模型(The Flexible Box Module),又叫FlexBox,意为"弹性布局",旨在通过弹性的方式来对 ...
- 利用Backtrace来捕获段错误堆栈信息
具体参考文档:https://blog.csdn.net/gatieme/article/details/84189280 测试Demo: #include <execinfo.h> #i ...
- QT 窗口拖拽移动实现
我们知道,要实现窗口移动可以直接鼠标点住窗口的标题栏实现拖拽移动,这是窗口默认的行为,在QT中的事件响应函数为moveEvent. 但是现实中经常需要鼠标点住窗口客户区域实现窗口的拖拽移动,代码实现如 ...