【问题标题】:Adding a password encryption key in ASP NET Core Identity在 ASP NET Core Identity 中添加密码加密密钥
【发布时间】:2019-11-25 06:41:07
【问题描述】:

在 ASP .NET Webforms 中,MachineKey(一个 guid)是数据库安全和应用程序安全的重要组成部分。它被用作表单身份验证密码的加密密钥,因此如果机器密钥被更改,现有的密码将不再被识别。 这确实为将数据库中的密码存储链接到网络服务器上的令牌提供了一定程度的安全性。 如果有多个网络服务器使用单个数据库身份验证存储,则它们都必须具有相同的机器 ID。

在 .NET Core MVC 中,我们现在在 Startup.cs 中声明了几个服务:

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                options => {
                    options.ExpireTimeSpan = new TimeSpan(0, 45, 0);
                    options.LoginPath = new PathString("/Account/Login");
                    options.AccessDeniedPath = new PathString("/Account/AccessDenied");
                }
            );

        services.AddDataProtection()
            .SetApplicationName("MyApplicationName")
            .PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"C:\server_config\my-app-keys"));

我查看了每个服务的方法,但没有一个提供数据存储加密密钥。我想也许 ApplicationName 可能被用作密钥,但如果我更改它,我仍然可以使用旧密码登录,所以它显然不是用来加密的。

本文将隔离规定为 API 的关键要求之一: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.0

这个有一个简单的例子,使用“目的字符串”在不同的逻辑存储之间提供加密隔离: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/using-data-protection?view=aspnetcore-3.0

但是我似乎无法将 .NET Core 中的密码加密与特定密钥联系起来。

在为标准 Asp Net Identity 注入设置特定的唯一加密密钥以用于存储在数据库中的密码方面,我缺少什么?

【问题讨论】:

  • 这能回答你的问题吗? How to implement machineKey in ASP.NET Core 2.0
  • 不,这实际上是我用来设置它们现在的方式的问题(即工作,但不完全是我想要的方式)。我已经坐了大约 9 个月了,我现在才处于微调阶段。
  • 实际上,也许链接到github.com/synercoder/FormsAuthentication的答案可能是一个解决方案。我希望有一些内置的东西。这是对旧行为的重大改变,我已经广泛搜索,我真的希望现在已经找到了一些东西。在SO上问这个问题是不得已的,我希望能找到这方面的大师。

标签: c# asp.net-core encryption asp.net-identity machinekey


【解决方案1】:

如果我没记错的话,您可以使用以下方法:

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.AspNet.DataProtection.Repositories;

namespace MySpace
{
    public class MyXmlRepository : IXmlRepository
    {
        public MyXmlRepository()
        {
            // Whatever I wanted injected in.
        }

        public IReadOnlyCollection<XElement> GetAllElements()
        {
            return null;
        }

        public void StoreElement(XElement element, string friendlyName)
        {
            // Persist something
        }
    }
}


如果您在 IOC 中注册它,它将开始将其用于密钥持久性,然后您可以做任何您喜欢的事情。

【讨论】:

  • 谢谢,但这是专门设置供 ASP NET 身份使用的加密密钥。我不想自己动手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多