gpt4 book ai didi

c# - 无法将 [] 索引应用于 IConfiguration 类型的表达式

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:51 26 4
gpt4 key购买 nike

我一直在尝试解决这个问题,但什么也想不起来了......
使用 token 的 Web 应用程序,但有些事情让我退缩。

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"]));

这是整个代码:
 namespace DutchTreat.Controllers
{
public class AccountController : Controller
{
private readonly ILogger<AccountController> _logger;
private readonly SignInManager<StoreUser> _signInManager;
private readonly UserManager<StoreUser> _userManager;
private readonly IConfiguration _config;

public AccountController(ILogger<AccountController> logger, SignInManager<StoreUser> signInManager, UserManager<StoreUser> userManager, IConfiguration config)
{
_logger = logger;
_signInManager = signInManager;
_userManager = userManager;
_config = config;
}
public IActionResult Login()
{
if (this.User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "App");
}
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, false);
if (result.Succeeded)
{
if (Request.Query.Keys.Contains("ReturnUrl"))
{
return Redirect(Request.Query["ReturnUrl"].First());
}
else
{
return RedirectToAction("Shop", "App");
}
}
}

ModelState.AddModelError("", "Failed to login");

return View();
}

[HttpGet]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "App");
}

[HttpPost]
public async Task<IActionResult> CreateToken([FromBody] LoginViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.UserName);
if (user != null)
{
var result = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);
if (result.Succeeded)
{
//Create the token
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.Email),
new Claim(JwtRegisteredClaimNames.Jti, new Guid().ToString()),
};

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"]));
}
}

}

return BadRequest();
}
}
}

最佳答案

如果这是 .NET Core,则有 GetValue<T>()函数,或 GetValue()如果要默认为字符串,则使用函数。

我使用它,因为我遇到了同样的错误。

引用这里 MSDN

但是在您的情况下它的基本用法是:
_config.GetValue<string>("Token:Key")
或者
_config.GetValue("Token:Key")

关于c# - 无法将 [] 索引应用于 IConfiguration 类型的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com