博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie
阅读量:7305 次
发布时间:2019-06-30

本文共 2710 字,大约阅读时间需要 9 分钟。

这是这两天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();

为了进一步确认密钥是否是一样的,修改了  的源代码将密钥打印在控制台,运行后确认2个站点用的密钥是一样的。

public IReadOnlyCollection
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
().AsReadOnly(); }}

后来突然想到 services.AddDataProtection() 是不是有什么配置选项?F12之后发现果然有个DataProtectionOptions:

public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action
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");

转载于:https://www.cnblogs.com/dudu/p/6495951.html

你可能感兴趣的文章
平台设计的变与不变 | 互联网平台建设(二十八)
查看>>
程序羊的2018年终总(gen)结(feng)
查看>>
万钢:我国新能源汽车处于过渡关键期,应有序推进产业发展 | 电动汽车百人会 2019...
查看>>
阿里云基础产品技术月刊 2019年3月
查看>>
罗永浩出售锤子空气净化器业务;Tim Cook 推特名改为 Tim Apple ;Windows 10 设备超过 8 亿 | 雷锋早报...
查看>>
Spark On HBase Idea远程调试
查看>>
常用设计模式 Java 实现
查看>>
Java编程之设计模式之工厂方法模式全解
查看>>
datax源码阅读三:JobContainer
查看>>
当AI自适应遇到语言学习,会颠覆我们学外语的方式吗?
查看>>
Java基础面试知识点总结
查看>>
消息队列常见的 5 个应用场景
查看>>
MobileForm控件的使用方式--用.NET(C#)开发APP的学习日志
查看>>
RabbitMQ的深入理解和最简单的用途说明
查看>>
JAVA 多用户商城系统b2b2c---配置中心和消息总线
查看>>
使用Kubespray部署Kubernetes集群
查看>>
Intellij IDEA 部署应用到容器服务 Kubernetes
查看>>
Google希望从搜索结果展现中了解搜索结果的内容
查看>>
Udacity并行计算课程笔记-The GPU Programming Model
查看>>
【对讲机的那点事】就在身边却不露面的朋友--无线电波
查看>>