日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie

發(fā)布時間:2023/12/4 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這是這兩天ASP.NET Core遷移中遇到的一個問題。2個ASP.NET Core站點(對應(yīng)于2個不同的ASP.NET Core Web應(yīng)用程序),2個站點都可以登錄,但在其中任1個站點登錄后,在當(dāng)前站點處于登錄狀態(tài),訪問另外1個站點卻處于未登錄狀態(tài)。

開始以為是CookieAuthenticationOptions的設(shè)置不一致引起的,檢查代碼后確認(rèn)AuthenticationScheme,CookieName,CookieDomain都是一樣的。

app.UseCookieAuthentication(new CookieAuthenticationOptions {CookieName = ".AspNetCore.Cookies",CookieDomain = ".cnblogs.com"});

(AuthenticationScheme的默認(rèn)值是"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();

為了進一步確認(rèn)密鑰是否是一樣的,修改了?DataProtection.DistributedStore?的源代碼將密鑰打印在控制臺,運行后確認(rèn)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之后發(fā)現(xiàn)果然有個DataProtectionOptions:

public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action<DataProtectionOptions> setupAction);

繼續(xù)F12發(fā)現(xiàn)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應(yīng)用程序要使用同樣的加解密方式,除了共享密鑰,還要設(shè)置同樣的ApplicationDiscriminator。

添加如下的代碼后問題立馬解決。

services.AddDataProtection(options => options.ApplicationDiscriminator = "cnblogs.com");


原文地址:http://www.cnblogs.com/dudu/p/6495951.html


.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注

總結(jié)

以上是生活随笔為你收集整理的DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。