这是这两天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的更多相关文章

  1. ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 设置和初始化数据库 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 设置和初始化数据库 上一章节中我们已经设置和配置好了 EF ...

  2. 用"hosting.json"配置ASP.NET Core站点的Hosting环境

    通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...

  3. ASP.NET Core WebApi中使用FluentValidation验证数据模型

    原文链接:Common features in ASP.NET Core 2.1 WebApi: Validation 作者:Anthony Giretti 译者:Lamond Lu 介绍 验证用户输 ...

  4. Cenos7 部署asp.net core站点

    系统版本 rpm -q centos-release --- centos-release--5.1804.el7.centos.x86_64 安装libicu yum install libunwi ...

  5. .NET跨平台之旅:在生产环境中上线第一个运行于Linux上的ASP.NET Core站点

    2016年7月10日,我们在生产环境中上线了第一个运行于Linux上的ASP.NET Core站点,这是一个简单的提供后端服务的ASP.NET Core Web API站点. 项目是在Windows上 ...

  6. .NET跨平台之旅:将QPS 100左右的ASP.NET Core站点部署到Linux服务器上

    今天下午我们将生产环境中一个单台服务器 QPS(每秒请求数)在100左右的 ASP.NET Core 站点部署到了 Linux 服务器上,这是我们解决了在 .NET Core 上使用 EnyimMem ...

  7. .NET跨平台之旅:生产环境中第2个跑在Linux上的ASP.NET Core站点

    今天我们在生产环境中上线了第2个跑在Linux上的ASP.NET Core站点.这是一个简单的Web API站点,通过命令行的方式调用安装在Linux服务器上的程序完成操作.之前用的是nodejs,现 ...

  8. .NET跨平台之旅:在Linux上以本地机器码(native)运行ASP.NET Core站点

    在将“.NET跨平台之旅”示例站点 about.cnblogs.com 从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 (博文链接)之后,我们有一个难以抗拒的冲动 —— 体验 ...

  9. 以self-contained方式在Linux上部署ASP.NET Core站点

    今天准备将一个在Windows上用VS2015开发的ASP.NET Core程序部署到阿里云Linux服务器上,部署时发现这台服务器是内网服务器,无法直接安装.NET Core SDK,于是想到尝试用 ...

随机推荐

  1. 实现一个原子的正整数类:AtomicPositiveInteger

    import java.util.concurrent.atomic.AtomicInteger; public class AtomicPositiveInteger extends Number ...

  2. <转>房租分配问题

    本文转自:https://blog.codingnow.com/2012/12/share_rent.html 今天读到策划同学的周报中提到的一个关于合租房子的分摊房租问题. 引用周报中的一节如下: ...

  3. 【ML】Predict and Constrain: Modeling Cardinality in Deep Structured Prediction -预测和约束:在深度结构化预测中建模基数

    [论文标题]Predict and Constrain: Modeling Cardinality in Deep Structured Prediction   (35th-ICML,PMLR) [ ...

  4. C#访问MySQL数据库的方法

    C#访问MySQL数据库的方法 (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 下载地址为: http://dev.mysql.com/downloads/connector/ne ...

  5. C# MD5 加密,解密

    //生成cs文件 public class MD5Help { ///MD5加密 public static string MD5Encrypt(string pToEncrypt, string s ...

  6. [Big Data - Kafka] Kafka设计解析(五):Kafka Benchmark

    性能测试及集群监控工具 Kafka提供了非常多有用的工具,如Kafka设计解析(三)- Kafka High Availability (下)中提到的运维类工具——Partition Reassign ...

  7. “RESOURCE MONITOR“CPU占用特别高

    背景: SQL Server 2008 R2 10.50.1600 没有设置页面文件,内存为64G,数据库分配50G cpu使用占了50%以上,平时只有10-20%,某台服务器“RESOURCE MO ...

  8. tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed

    每次重启自己的服务tomcat都需要卡住很长时间,每次都是日志停在 Root WebApplicationContext: initialization completed in 744 ms这个地方 ...

  9. CAP原理中的一致性

    CAP原理指的是,这三个要素最多只能同时实现两点,不可能三者兼顾.因此在进行分布式架构设计时,必须做出取舍.而对于分布式数据系统,分区容忍性是基本要求,否则就失去了价值.因此设计分布式数据系统,就是在 ...

  10. PLSQL存储过程(基础篇)-转

    我不是专门的开发人员,但存储过程又是很重要的知识,为了能够很好的记忆,现把这些基础知识总结一下.存储过程可以实现代码的充分共享,提高系统性能. 基础篇       知识回顾 如果经常使用特定操作,哪么 ...