【问题标题】:ASP.NET MVC 5 Authorization IssuesASP.NET MVC 5 授权问题
【发布时间】:2018-07-11 05:40:20
【问题描述】:

我正在处理一个项目,需要为几个功能添加授权。目前,设置非常简单。我要做的就是登录并获得授权。但是,登录功能成功,而 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);
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多