c# - 如何使用 T-SQL 插入 Identity Server 4 持久化的 ApiSecret 值

标签 c# hash identityserver4

我已经通过 Identity Server 4 QuickStart 了解如何使用 Entity Framework 持久存储配置和操作数据。在 QuickStart 中,ApiResources 以代码的形式加载到数据库中。 Api secret 设置为

        new ApiResource("api1", "My API")
        {
            ApiSecrets = { new Secret("secret".Sha256())}
        }

在 ApiResource 构造函数中。当在 Startup.InitializeDatabase 中,ApiResource 被添加到 ConfigurationDbContext.ApiResources DbSet 时,

        foreach(var resource in Config.GetApiResources())
        {
            context.ApiResources.Add(resource.ToEntity());
        }
        context.SaveChanges();

子 ApiSecrets 表中的记录在 ApiSecrets.Value 字段中包含一个可读的文本值。

    K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=

我想通过 SQL 脚本管理我的配置数据,但我不知道如何正确设置 ApiSecrets.Value。我试过使用 T-SQL HASHBYTES('SHA2_256', 'secret'),但这会在 ApiSecrets.Value 中产生一个不可读的(我认为是二进制的)值。有没有办法通过 T-SQL 正确设置哈希密码?

最佳答案

您使用HASHBYTES 的方向是正确的,只需要从BinaryHash 中获取Base64 哈希:

DECLARE @HASHBYTES VARBINARY(128) = hashbytes('sha2_256', 'secret')
SELECT cast(N'' as xml).value('xs:base64Binary(sql:variable("@HASHBYTES"))', 'varchar(128)');

关于c# - 如何使用 T-SQL 插入 Identity Server 4 持久化的 ApiSecret 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207257/

相关文章:

c# - 如何通过引用将包含字符串的结构从 C# 发送到非托管 C++ 库

perl - 使用 Perl 从文本文件中提取和打印键值对

oracle - ORA_HASH函数使用的算法是什么?

asp.net-mvc - IdentityServer 4、OpenIdConnect 重定向到外部登录 url

asp.net-core-mvc - ASP.NET Core v1.1 中的 IdentityServer 和简单注入(inject)器集成

c# - CLR 中的托管堆

c# - 在 Blazor wasm 中使用 CSharpScript 时出现 System.IO.FileNotFoundException

.Net Core 2.1 与 IdentityServer4 Cookie 和基于 API JWT 的同一应用程序身份验证

C# ASP.NET MVC PayPal 找不到程序集

security - 客户端密码哈希与纯文本