首页 > 解决方案 > 在 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。

标签: c#asp.net-core.net-core

解决方案


在默认设置中,它存储在项目内的 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


推荐阅读