无法登录到 asp.net MVC Web 应用程序



我有以下Login方法(POST)为用户登录

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View("MainDashboard", "Home", model);
        }
    }

但是一旦我插入正确的凭据(电子邮件和密码),我就无法登录,我得到了

找不到视图"主仪表板"或其主节点或没有视图引擎 支持搜索的位置。以下位置是 搜索:

错误

我在同一个AccountController类中SignInManager

方法如下
public class AccountController : Controller
{
    private ProjectEntities db = new ProjectEntities();
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;
    private ApplicationRoleManager _roleManager;
    public AccountController()
    {
    }            
    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ApplicationRoleManager roleManager)
    {
        UserManager = userManager;
        RoleManager = roleManager;
        SignInManager = signInManager;
    }
    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set 
        { 
            _signInManager = value; 
        }
    }
    public ApplicationUserManager UserManager
    {
        ...
    }
    public ApplicationRoleManager RoleManager
    {
        ...
    }

如果我正在阅读您尝试正确执行的操作,您希望在登录后转到 MainDashboard。如果是这种情况,则登录操作结果的逻辑应如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    // Return to Login View if there is an invalid model submitted (with errors displayed)
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    // Attempt to Login
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        // If Login is successful
        case SignInStatus.Success:
            // Go to the returnUrl if there is a returnUrl (used when User has been automatically signed out or has tried to hit an unauthorized page)
            if(!string.IsNullOrEmpty(returnUrl))
            {
                return RedirectToLocal(returnUrl);
            } else
            {
               // Go to MainDashboard if there is no returnUrl and Login is successful (without referencing the LoginViewModel)
                return RedirectToAction("MainDashboard", "Home");
            }
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            // If Login has failed, and it's not due to a Lockout and not because the User has to Verify the Registration code on Registration
            // Return to the Login View with errors
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

此外,显然,请确保在主控制器中有一个针对主仪表板的视图和控制器操作。我可能读错了你想要的东西,但如果这是你想要实现的,这是可以做到的一种方法。

相关内容

  • 没有找到相关文章

最新更新