繁体   English   中英

ASP.NET MVC 5授权问题

[英]ASP.NET MVC 5 Authorization Issues

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

似乎Startup.cs中app.UserAuthentication()和App.UseMvcWithDefaultRoute()的顺序很重要。 当我将UseAuthentication放在路由材料之前时,它突然起作用了。 某种意义上说在路由过程中需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM