我一直在尝试解决此问题,但现在没有任何想法... 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();
}
}
}
答案 0 :(得分:2)
_config.Item["Tokens:Key"]
几乎是合法的任务,错误似乎是Iconfiguration
引用了AutoMapper.Configuration
而不是Microsoft.Extensions.Configuration
答案 1 :(得分:2)
我遵循相同的命令检查您的using语句,并确保您的IConfiguration来自
Microsoft.Extensions.Configuration
而不是
Automapper.Configuration
答案 2 :(得分:1)
一个例外是,您无法使用# deep copy
from copy import deepcopy
reversedmatrix = [deepcopy(row) for row in reversed(matrix)]
的索引([]
)
您正在寻找IConfiguration
吗?
答案 3 :(得分:0)
如果是.NET Core,则有一个flexDirection: 'column', justifyContent: 'flex-end'
函数,或者,如果您想默认为字符串,则有一个GetValue<T>()
函数。
我使用这个是因为我遇到了同样的错误。
请在此处MSDN
在您的情况下,它的基本用法是:
GetValue()
或
_config.GetValue<string>("Token:Key")
答案 4 :(得分:0)
我之所以这样发布是因为,如果您不小心在dotnet核心中为IConfiguration使用了错误的命名空间,那么您可能会在此处收到相同的错误消息。
使用:
using Microsoft.Extensions.Configuration;
代替:
using Castle.Core.Configuration;