asp.net-core - .NET Core 3.1 中的机器密钥实现
问题描述
在 ASP.NET 中,我们通常会像这样将机器密钥添加到 web.config -
<machineKey validation="HMACSHA512" decryption="AES" validationKey="********" decryptionKey="******" />
有人可以告诉我如何在 ASP.NET Core 3.1 中做同样的事情吗?
解决方案
要为站点上的多个 Web 应用程序启用 SSO(单点登录),请确保在所有 Web 应用程序的 Startup.cs 中添加 DataProtection 并设置相同的 ApplicationName,将密钥保存到相同的目录并使用相同的 cookie 名称像这样:
services
.AddDataProtection()
.SetApplicationName("MyWebSite")
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\MyWebSite-keys"));
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = "/login/login";
options.Cookie.Path = "/";
options.Cookie.Name = "MyWebSite-Login";
});
在 Core 中不再使用 machineKey,而不是 validationKey 和 decryptionKey,只会使用 masterkey,并且会在您指定的目录中自动创建。
在此之后,在登录应用程序 A 并请求应用程序 B 后,用户将保持登录状态。
推荐阅读
- api - nodejs robinhood api登录
- google-bigquery - 气流 BigQueryInsertJobOperator 配置
- python - Python中嵌套循环的问题
- azure - 是否可以使用给定公共 IpPrefix 的静态 IP 修改 VMScaleset 实例的公共 IP?
- arrays - 如何在 Swift 中将 [String] 划分为指定数量的相等部分?
- json.net - 使用 AnyOf/OneOf/AllOf 生成 JSON 模式
- security - 如何修复 npm 漏洞?
- python - 如何使用嵌套字典进行长度检查
- azure - 尝试使用专用端点和专用链接运行用于存储帐户配置的 ARM 模板时出错
- flutter - 我想用未来的布尔值保存列表项是否可能?