简体   繁体   English

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

[英]In ASP.net Identity, how do you redirect a user to a specific url after login?

I'm use the ASP.Net identity 2.0 package for my application. 我正在为我的应用程序使用ASP.Net Identity 2.0程序包。 After a user successfully logs in, I want to redirect them to a specific URL on the site. 用户成功登录后,我想将他们重定向到站点上的特定URL。 Presently, after successful authorization, it sends them back to the default index page. 当前,在成功授权之后,它将它们发送回默认索引页面。

I think it's somewhere in this section of AccountController.cs: 认为它位于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);
        }
    }

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

But this means you are ignoring the returnUrl . 但这意味着您将忽略returnUrl

我使用Response.Redirect("~/SomePage", false);

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

相关问题 在ASP.NET Core上使用Identity登录后,如何直接获取用户详细信息? - How do I get the user details straight after login using Identity on ASP.NET Core? 如何使用AspNet.Identity使用Asp.Net MVC5 RTM位登录/验证用户? - How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity? 登录后的ASP.NET身份用户为null - ASP.NET Identity user null after login ASP.NET身份-更新用户记录后保持登录 - ASP.NET Identity - Maintain login after updating user record 在 ASP.NET 内核中,你在哪里配置重定向登录? - In ASP.NET Core, where do you configure redirect to login? 如何在当前用户的ASP.NET Identity 2中获取用户配置文件值? - How do you obtain user profile values in ASP.NET Identity 2 of current user? ASP.NET与登录提示交互后获取重定向的URL - ASP.NET getting the URL of a redirect after interacting with a login prompt asp.net身份多用户登录 - asp.net identity multipe user login 如果用户使用ASP.NET直接在浏览器中输入URL,如何重定向到登录页面 - how to redirect to login page if user directly entres URL in browser using ASP.NET 如何将特定的 URL 重定向到 ASP.NET 中的另一个 URL? - How to redirect a specific URL to another URL in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM