使用Identity Aspnetcore 2.0设置Dapper

时间:2017-09-30 09:20:38

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

我发现似乎是一个非常整洁的小包,可以让你使用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部分。它是否希望我保持原样,或者是否希望我提供某种加密字符串。我只是不明白。另一部分我很困惑的是将Connection字符串保留为可读格式,我觉得我应该加密它,把它放在ConnectionString部分然后提供一个密钥来解密它?

1 个答案:

答案 0 :(得分:2)

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

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

示例:

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

如果您要使用加密部分,则需要为AES生成一对Key和IV,使用它加密您的db密码,然后将Key和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
}