AptNetCore使用Secrets管理私密数据

前言

在项目中, 数据库连接, 账户及密码等如果存储在appsetting.json中就太不安全了, 所以生产时都是放在环境变量中读取的.

在开发中可能存在每一台开发机用到的一些变量都不一样的情况, 这个时候如果写在appsettings.Development.json中每次提交版本控制就不方便了.

所以dotnet-cli贴心的提供了 user-secrets 命令, 来管理开始时用户的私密数据.

使用

设置UserSecretsId

在项目根目录输入 dotnet user-secrets list, 可以看到错误提示

Could not find the global property 'UserSecretsId' in MSBuild project '/home/xueyou/website-demo/website-demo.csproj'. Ensure this property is set in the project or use the '--id' command line option.

这提示我们, 要想使用Secrets管理机密, 需先定义UserSecretsId

并且根据上面的提示可以知道, 它在.csproj文件中寻找UserSecretsId, 那我们就在此文件中定义UserSecretsId

编辑.csproj文件在PropertyGroup内增加<UserSecretsId>79a3edd0-2092-40a2-a04d-dcb46d5ca9ed</UserSecretsId>

UserSecretsId值是GUID生成的, 每一个值都会实际对应到文件夹的名称上

  • windows中, %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json
  • linux中, ~/.microsoft/usersecrets/<user_secrets_id>/secrets.json

设置机密

dotnet user-secrets set "WeChatAppKey" "X3423FEED2435DD"

其中keyWeChatAppKey是dotnet core配置系统中的key, 所以可以是:号分隔, 映射到配置树

dotnet user-secrets list 可以查看当前机密

代码中访问机密

public class Startup
{
private string _wechatKey= null; public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
_wechatKey = Configuration["WeChatAppKey"];
} public void Configure(IApplicationBuilder app)
{
var result = string.IsNullOrEmpty(_wechatKey) ? "Null" : "Not Null";
app.Run(async (context) =>
{
await context.Response.WriteAsync($"Secret is {result}");
});
}
}

脚注

[ASP.NET Core 优雅的在开发环境保存机密](https://www.cnblogs.com/savorboard/p/dotnetcore-user-secrets.html)

在开发期间安全存储应用机密

ubuntu中使用机密数据Secrets的更多相关文章

  1. 解决Ubuntu中phpmyadmin对数据上传上限2M

    本文部分参考自:http://www.myhack58.com/Article/sort099/sort0102/2011/29396.htm 原文有少量错误或者过时的(相对于ubuntu15来说)内 ...

  2. 在Asp.NET Core中如何优雅的管理用户机密数据

    在Asp.NET Core中如何优雅的管理用户机密数据 背景 回顾 在软件开发过程中,使用配置文件来管理某些对应用程序运行中需要使用的参数是常见的作法.在早期VB/VB.NET时代,经常使用.ini文 ...

  3. ubuntu 中数据的迁移

    1.先停止mysql /etc/init.d/mysql stop

  4. Ubuntu中使用pyUSB读取鼠标或键盘的数据程序

    参考 :http://www.orangecoat.com/how-to/read-and-decode-data-from-your-mouse-using-this-pyusb-hack 要注意的 ...

  5. Ubuntu中实现Docker内安装jenkins+jenkins远程触发

    前面做了在ubuntu中安装jenkins+docker实现自动部署,但是得安装jdk8+tomcat8环境,比较麻烦,因此本文记录如何将jenkins直接装在dockers内并且实现远程触发功能. ...

  6. Ubuntu 中软件的安装、卸载以及查看的方法总结

    Ubuntu 中软件的安装.卸载以及查看的方法总结 博客分类: Linux UbuntuDebian配置管理CacheF#  说明:由于图形化界面方法(如Add/Remove... 和Synaptic ...

  7. 在Ubuntu中安装Redis

    原文地址:http://blog.fens.me/linux-redis-install/ 在Ubuntu中安装Redis R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库.涉及的 ...

  8. Ubuntu中查看硬盘分区UUID的方法(所有Linux目录的解释)

    在Ubuntu中UUID的两种获取方法,至于UUID是什么,你可以大概理解为分区的标识符,像条形码那样. 在终端中输入下面的命令就可心查看到分区UUID了.命令1.sudo blkid 命令2.ls ...

  9. Ubuntu中使用iptables

    (一) 设置开机启动iptables # sysv-rc-conf --level 2345 iptables on (二) iptables的基本命令 1. 列出当前iptables的策略和规则 # ...

随机推荐

  1. [Swift]LeetCode199. 二叉树的右视图 | Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  2. [Swift]LeetCode967. 连续差相同的数字 | Numbers With Same Consecutive Differences

    Return all non-negative integers of length N such that the absolute difference between every two con ...

  3. 关于 redis.properties配置文件及rule

    当你配置的 FinanceRlue 的path为/count/users时,redis.properties中就必须也配置一个/users=redis://localhost:6379/2?keyle ...

  4. 网络协议 5 - ICMP 与 ping:投石问路的侦察兵

        日常开发中,我们经常会碰到查询网络是否畅通以及域名对应 IP 地址等小需求,这时候用的最多的应该就是 ping 命令了. 那你知道 ping 命令是怎么工作的吗?今天,我们就来一起认识下 pi ...

  5. 一个类是怎么被JVM执行的

    现有如下代码,那么我们的JVM是怎么执行的呢 public class Test{ public static vodi main(String[] args){ MaYun my=new MaYun ...

  6. pyhive 连接 Hive 时错误

    一.User: xx is not allowed to impersonate xxx' 解决办法:修改 core-site.xml 文件,加入下面的内容后重启 hadoop. <proper ...

  7. MyEclipse 新手使用教程---图文详解

    引言 某天在群里看到有小伙伴问MyEclipse/Eclipse的一些使用问题,虽然在我看来,问的问题很简单,但是如果对于刚刚学习的人来说,可能使用就不那么友好了.毕竟我在开始使用MyEclipse/ ...

  8. Linux驱动模块编译模板

    hello.c文件: #include <linux/module.h> #include <linux/kernel.h> static int hello_init(voi ...

  9. leetcode — reverse-linked-list

    /** * Source : https://leetcode.com/problems/reverse-linked-list/ * * * Reverse a singly linked list ...

  10. man帮助文档打印

    这里不讨论大家都知道的man重定向的一般常用方法(col处理方法)$ man find | col -b > man_fine.txt [跟着我的思路走]假如您像我一样,直接使用如下命令导出fi ...