【发布时间】:2020-03-21 00:51:29
【问题描述】:
我正在尝试创建自己的登录身份验证,但它不起作用,所以我创建了这个快速测试...
[HttpGet]
public IActionResult Index()
{
try
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, "Test Name"));
var principal = new ClaimsPrincipal(identity);
SignIn(principal, CookieAuthenticationDefaults.AuthenticationScheme);
var isAuthenticated = User.Identity.IsAuthenticated; //-- THIS IS ALWAY FALSE... BUI JUST LOGGED IN?!?!?!?
return View();
}
catch(Exception ex)
{
_logger.LogError(ex, "Error:");
return StatusCode(500);
}
}
所以你可以看到我调用SignIn 然后在下一行我检查用户现在是否已通过身份验证,但它总是返回 false,User.Identity 也看起来是空的。有谁知道我在这里做错了什么?
在我的 Startup / ConfigureServices 我有:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = "/Admin/Index";
});
在启动/配置中
app.UseAuthentication();
app.UseCookiePolicy(new CookiePolicyOptions
{
MinimumSameSitePolicy = SameSiteMode.Strict,
});
【问题讨论】:
标签: c# authentication asp.net-core asp.net-core-2.2