【问题标题】:Setting up Dapper with Identity Aspnetcore 2.0使用 Identity Aspnetcore 2.0 设置 Dapper
【发布时间】:2018-02-02 00:33:47
【问题描述】:

我发现了一个看起来非常简洁的小包,可以让您使用 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 部分,然后提供解密它的密钥?

【问题讨论】:

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


    【解决方案1】:

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

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

    例子:

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

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

    【讨论】:

    • 如何为 AES 生成一对 Key 和 IV?这对我来说是新的。
    • 方法太多了,我能想到的最直接的就是使用OpenSSL。但如果您愿意,您也可以使用以下方法在 .NET 4+ 中创建它们:stackoverflow.com/a/2512373/3670737
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2021-06-21
    • 2018-02-20
    • 2018-10-19
    • 2015-01-07
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多