如何使用T-SQL插入Identity Server 4的持久化ApiSecret值 [英] How to insert Identity Server 4 persisted ApiSecret values using T-SQL

查看:83
本文介绍了如何使用T-SQL插入Identity Server 4的持久化ApiSecret值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过了Identity Server 4快速入门,可以使用Entity Framework持久存储配置和操作数据.在快速入门中,将ApiResources通过代码加载到数据库中. Api机密设置为

I have been through the Identity Server 4 QuickStart for using Entity Framework for persistent storage of configuration and operational data. In the QuickStart, the ApiResources are loaded into the database in code. The Api secret is set with

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

ApiResource构造函数中的

.在Startup.InitializeDatabase中,将该ApiResource添加到ConfigurationDbContext.ApiResources DbSet时,

in the ApiResource constructor. When, in Startup.InitializeDatabase, that ApiResource is added to the ConfigurationDbContext.ApiResources DbSet,

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

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

the record in the child ApiSecrets table contains a readable text value in the ApiSecrets.Value field.

    K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=

我想通过SQL脚本管理我的配置数据,但是我不知道如何正确设置ApiSecrets.Value.我尝试使用T-SQL HASHBYTES('SHA2_256','secret'),但是在ApiSecrets.Value中产生了不可读的(我认为是二进制)值.有没有一种方法可以通过T-SQL正确设置散列机密?

I would like to manage my configuration data through SQL scripts, but I can't figure out how to set the ApiSecrets.Value correctly. I've tried using T-SQL HASHBYTES('SHA2_256', 'secret'), but that produces an unreadable (I think binary) value in ApiSecrets.Value. Is there a way to set the hashed secret correctly through T-SQL?

推荐答案

您在使用HASHBYTES的正确轨道上,只需要从BinaryHash中获取Base64哈希即可:

You were on the right track to use HASHBYTES, just need to get the Base64 hash out of the BinaryHash:

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

这篇关于如何使用T-SQL插入Identity Server 4的持久化ApiSecret值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆