c# - 在 Angular 7 和 net 2.1 中使用 Secret String 进行身份验证
问题描述
我正在关注本教程“Angular 7 - 用户注册和登录示例和教程”。我想知道 app.Settings.secret 的存储位置。下面显示了 appSettings 是如何声明的。我找不到“秘密”字符串的存储位置
public UsersController(
IUserService userService,
IMapper mapper,
IOptions<AppSettings> appSettings)
{
_userService = userService;
_mapper = mapper;
_appSettings = appSettings.Value;
}
这是 appSettings.cs 的 poco
public class AppSettings
{
public string Secret { get; set; }
}
最后,这是在 UsersController 中访问它的方式:
public IActionResult Authenticate([FromBody]UserDto userDto)
{
var user = _userService.Authenticate(userDto.Username, userDto.Password);
if (user == null)
return BadRequest(new { message = "Username or password is incorrect" });
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
当我使用真正的后端运行程序并在此行上使用断点时:
---> var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
和 mouseover _appSettings.Secret 我看到这个字符串:
“这用于签署和验证 JWT 令牌,用你自己的秘密替换它,它可以是任何字符串”
我想知道字符串存储在哪里以分配给_appSettings.Secret。
解决方案
在默认设置中,它存储在项目内的 appsettings[.EnvirnomentName (optional)].json 文件中,或者来自 asp.net 核心配置包支持的各种其他来源:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2
这是“选项模式”的文档:https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2
推荐阅读
- ssl - PEM 文件(导出证书 - 在 Postman 中受信任)
- python - 尝试调用变量以返回脚本的开头,但出现错误:“TypeError:'NoneType' 对象不可调用”
- wikibase - 更改 Wikibase 中呈现的属性的顺序
- c++ - 为什么在此异常处理代码中不将 0 视为 int ?
- html - 勾选复选框时如何删除输入文本?
- sql - 删除 '-' 之前和之后的字符 '-' 保留包含多个字符 '-' 的字符串中间
- javascript - 如何在保持“必需”功能不变的同时将“发送”按钮链接到另一个页面
- rest - 为什么我不能以这种方式在 request.get() 中使用 parse.quote() ?
- python - Python:将一行展开为多行并进行计算
- azure-virtual-machine - Azure VM 的动态专用 IP 地址的 TTL?