ASP.NET MVC 5授权问题

时间:2018-02-01 00:21:31

标签: c# asp.net-mvc authentication asp.net-mvc-5

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

我该如何解决这个问题?我应该检查 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);
}

1 个答案:

答案 0 :(得分:1)

看起来,Startup.cs中的app.UserAuthentication()和App.UseMvcWithDefaultRoute()的顺序很重要。当我在路由之前放置UseAuthentication时,它突然起作用了。在路由期间需要它是有道理的。

相关问题