C# ASP.NET MVC 5授权问题

C# ASP.NET MVC 5授权问题,c#,asp.net-mvc,authentication,asp.net-mvc-5,C#,Asp.net Mvc,Authentication,Asp.net Mvc 5,我正在处理一个项目,需要为几个功能添加授权。目前,设置非常简单。我只想登录并获得授权。但是,登录功能成功,而User.Identity.IsAuthenticated保持设置为false,导致无法访问标记为[Authorize]的任何内容 我该如何着手解决这个问题?我是否应该检查DBO.AspNetUserLogins中的条目以查看登录是否已被记录 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task&l

我正在处理一个项目,需要为几个功能添加授权。目前,设置非常简单。我只想登录并获得授权。但是,登录功能成功,而User.Identity.IsAuthenticated保持设置为false,导致无法访问标记为[Authorize]的任何内容

我该如何着手解决这个问题?我是否应该检查DBO.AspNetUserLogins中的条目以查看登录是否已被记录

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel details, string returnUrl)
{
    if (ModelState.IsValid)
    {
        AppUser user = await userManager.FindByEmailAsync(details.Email);
        if (user != null)
        {
            await signInManager.SignOutAsync();
            Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(user, details.Password, false, false);
            if (result.Succeeded)
            {
                if (User.Identity.IsAuthenticated)
                {
                   //Just curious if this is true.
                }
                return Redirect(returnUrl);
            }
        }

        ModelState.AddModelError(nameof(LoginViewModel.Email), " Invalid user or password");
    }

    return View(details);
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginViewModel详细信息,字符串返回URL)
{
if(ModelState.IsValid)
{
AppUser=await userManager.findbyemailsync(details.Email);
如果(用户!=null)
{
等待signInManager.SignOutAsync();
Microsoft.AspNetCore.Identity.SignInResult result=wait-signInManager.PasswordSignInAsync(用户,详细信息。密码,false,false);
if(result.successed)
{
if(User.Identity.IsAuthenticated)
{
//只是好奇这是不是真的。
}
返回重定向(returnUrl);
}
}
ModelState.addmodeleror(nameof(LoginViewModel.Email),“无效用户或密码”);
}
返回视图(详细信息);
}

似乎Startup.cs中app.UserAuthentication()和app.UseMvcWithDefaultRoute()的顺序很重要。当我把UseAuthentication放在route之前时,它突然起作用了。在路由过程中需要它,这有点道理