C# 无法登录到asp.net mvc web应用程序

C# 无法登录到asp.net mvc web应用程序,c#,asp.net-mvc,login,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc,Login,Asp.net Mvc 5,Asp.net Identity,我有以下Login方法(POST)为用户登录 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return Vie

我有以下
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);
        }
    }

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

[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);
    }
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginView模型,字符串返回URL)
{
//如果提交的模型无效(显示错误),则返回登录视图
如果(!ModelState.IsValid)
{
返回视图(模型);
}
//尝试登录
var result=wait SignInManager.PasswordSignInAsync(model.Email、model.Password、model.RememberMe、shouldllockout:false);
开关(结果)
{
//如果登录成功
案例标志状态成功:
//如果存在returnUrl,请转到returnUrl(当用户已自动注销或试图访问未经授权的页面时使用)
如果(!string.IsNullOrEmpty(returnUrl))
{
返回重定向到本地(returnUrl);
}否则
{
//如果没有返回URL且登录成功(不参考LoginViewModel),请转到MainDashboard
返回重定向到操作(“主仪表板”、“主页”);
}
案例标志状态锁定输出:
返回视图(“锁定”);
案例标志状态。要求验证:
return RedirectToAction(“SendCode”,new{ReturnUrl=ReturnUrl,RememberMe=model.RememberMe});
案例信号状态故障:
违约:
//如果登录失败,并且不是由于锁定,也不是因为用户必须在注册时验证注册码
//返回到登录视图,但出现错误
AddModelError(“,”登录尝试无效“);
返回视图(模型);
}
}

此外,显然,请确保主控制器中有MainDashboard的视图和控制器操作。我可能读错了您想要的内容,但如果这是您想要实现的,那么这是一种可以实现的方法。

问题不在于登录。PasswordSignInAsync可能返回失败,但随后返回错误ViewResult@tmg调试后,我可以看到
var result
变得失败,这是什么原因?
[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);
    }
}