c# - 在 ASP.net Identity 中,如何在登录后将用户重定向到特定的 url?

标签 c# asp.net asp.net-mvc asp.net-identity-2

我正在为我的应用程序使用 ASP.Net identity 2.0 包。用户成功登录后,我想将他们重定向到站点上的特定 URL。目前,授权成功后,将它们发送回默认索引页面。

认为它在 AccountController.cs 的这一部分中的某处:

 //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doen't count login failures towards lockout only two factor authentication
        // To enable password failures to trigger lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
        var user = await UserManager.FindByNameAsync(model.UserName);

        switch (result)
        {
            case SignInStatus.Success:
                if (user.LoginStage <= 0)
                {
                    return RedirectToAction("EnableGoogleAuthenticator", "Manage");
                }
                else
                {
                    return RedirectToLocal(returnUrl);
                }
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
                return View(model);
        }
    }

最佳答案

RedirectToLocal(returnUrl); 更改为 return RedirectToAction("Action", "Controller");

但这意味着您忽略了 returnUrl

关于c# - 在 ASP.net Identity 中,如何在登录后将用户重定向到特定的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589191/

相关文章:

asp.net-mvc - ASP.NET MVC 标识 :how do I Extend AspNetUserRoles table

c# - 如何在 Entity Framework 中的 DbSet 属性上设置过滤选项?

c# - 无法从 youtube 上传中提取视频

asp.net - 由于 z-index,来自用户控件的 devexpress 弹出控件呈现在新闻面板后面

asp.net - 如何在 web api 2、odata 中隐藏元数据

asp.net-mvc - ASP.NET MVC。实现持久排序的干净方法?

c# - C#中异步调用方法

c# - 如何使用 Web api 和 Angular 从字节下载文件

.net - 将 SSRS .RDL 渲染为 PDF 并打开它

c# - Azure 上的 EF6 连接到远程 MySQL DB 时出现间歇性问题