Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET核心页在登录之前返回_C#_Asp.net Core - Fatal编程技术网

C# ASP.NET核心页在登录之前返回

C# ASP.NET核心页在登录之前返回,c#,asp.net-core,C#,Asp.net Core,我正在开发一个带有cookies身份验证的.NET Core 1.1 ASP.NET应用程序,我注意到有点奇怪。我注意到,当我点击我的登录页面时,它会说我没有经过身份验证,即使这是操作所做的唯一事情。如果我刷新页面或导航到另一个页面,我将通过身份验证。似乎在异步登录之前就返回了视图 控制器方法 public async Task<IActionResult> Login() { ClaimsPrincipal userPrincipal = new ClaimsPri

我正在开发一个带有cookies身份验证的.NET Core 1.1 ASP.NET应用程序,我注意到有点奇怪。我注意到,当我点击我的
登录
页面时,它会说我没有经过身份验证,即使这是操作所做的唯一事情。如果我刷新页面或导航到另一个页面,我将通过身份验证。似乎在异步登录之前就返回了视图

控制器方法

public async Task<IActionResult> Login()
{
        ClaimsPrincipal userPrincipal = new ClaimsPrincipal(/*Setup the principal*/);

        await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
        {
            ExpiresUtc = DateTime.UtcNow.AddMinutes(5),
            IsPersistent = false,
            AllowRefresh = false
        });

    return View();
}
公共异步任务登录()
{
ClaimsPrincipal userPrincipal=新建ClaimsPrincipal(/*设置主体*/);
等待HttpContext.Authentication.SignInAsync(“Cookie”,用户主体,新的AuthenticationProperties
{
ExpiresUtc=DateTime.UtcNow.AddMinutes(5),
ispersist=false,
AllowRefresh=false
});
返回视图();
}
查看

@if (this.User.Identity.IsAuthenticated == true)
{
    <span>You're authenticated!</span>
}
else
{
    <span>You're not authenticated.</span>
}
@if(this.User.Identity.IsAuthenticated==true)
{
你被认证了!
}
其他的
{
你没有经过认证。
}

如果我导航到
/Controller/Login
我会发现我没有经过身份验证,但是如果我使用相同的代码转到另一个视图,它会说我经过身份验证。这不是什么大问题,但我担心我可能设置不正确。

这是故意的。调用
SignInAsync
后,必须执行重定向,例如
RedirectToAction
,因为只有在下次请求服务器时才会重建用户对象。

很有趣,谢谢!有关于这方面的文档吗?我没有任何官方文档的链接,但这很有意义,因为User.Identity用于当前请求,SignInAsync修改响应,以便新构造的cookie将与下一个请求一起发送。