使用 Identity Aspnetcore 2.0 设置 Dapper

Setting up Dapper with Identity Aspnetcore 2.0

我发现了一个看起来非常简洁的小程序包,可以让您使用 dapper + identity core 2.0。但是,由于我对核心开发还很陌生,所以我对某些事情感到有些困惑,而且我不确定如何解决它。有问题的包裹是这样的:https://github.com/grandchamp/Identity.Dapper

程序包要求我设置一些小的配置,然后就可以正常工作了。以下是说明:

//To configure the DBMS connection, you can add a DapperIdentity and a DapperIdentityCryptography section to your configuration file like this:

"DapperIdentity": {
    "ConnectionString": "Connection string of your database",
    "Username": "user",
    "Password": "123"
},
"DapperIdentityCryptography": {
    "Key": "base64 32 bits key",
    "IV": "base64 16 bits key"
}

真正让我困惑的是 DapperIdentityCryptography 部分。它是希望我保持原样还是希望我提供某种加密字符串。我只是不明白。我感到困惑的另一部分是以可读格式保留连接字符串,我觉得我应该加密它,将它放在 ConnectionString 部分,然后提供一个密钥来解密它?

据我目前所见,DapperIdentityCryptography 部分仅用于解密 DapperIdentity 部分中提供的 Password。这不是很有用,因为您将加密密码和加密密钥都存储在同一个文件中,这与以明文形式存储密码几乎相同。

您也可以直接以纯文本形式存储密码,完全忽略 DapperIdentityCryptography 部分。

示例:

"DapperIdentity": {
    "ConnectionString": "Server=myServerAddress;Database=myDataBase",
    "Username": "MyUserName",
    "Password": "MyPassword"
}

相反,如果您想使用加密部分,您需要为 AES 生成一对密钥和 IV,用它加密您的数据库密码,然后将密钥和 IV 存储在您的 appsettings.json 转换为 BASE64 字符串(或者您可以在您的开发机器中使用命令行实用程序 dotnet user-secrets,如文档所述)并将您的 Db 密码转换为 UTF8 字符串。

那么你的 appsettings.json 可能看起来像这样(取自样本):

"DapperIdentity": {
    "ConnectionString": "Server=myServerAddress;Database=myDataBase",
    "Username": "MyUserName",
    "Password": "MYUTF8STRINGENCRYPTEDPASSWORD"
},
"DapperIdentityCryptography": {
    "Key": "FrFE/VtQ5pfNhGYVnyf65Sa6j4h6ion3ItkAnqLsnBE=", // this is an example, never use in production
    "IV": "Ig/YU0tgUqI1u2VzWH0plQ==" // this is an example, never use in production
}