成功登录后如何重定向到登录发生的同一区域 [英] How To redirect to same area where login happen after successfully login

查看:82
本文介绍了成功登录后如何重定向到登录发生的同一区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Hotpots]
public async Task<IActionResult> Login(LoginViewModel loginViewModel,string area)
   {
     if (ModelState.IsValid)
            {
         var result = await signInManager.PasswordSignInAsync(loginViewModel.Email,loginViewModel.Password,oginViewModel.RememberMe, false);
                if (result. Succeeded)
                {
                    return RedirectToAction("index", "home", new { area=area});
                }
                ModelState.AddModelError(string. Empty, "Invalid Login Attempt");
            }
            return View(LoginViewModel);
     }


Now After Successful login I want to redirect to same area how to do that 

推荐答案

仅在'Login'操作中添加字符串参数'area'便无法直接实现重定向.相反,您应该在登录"过程中传递区域值

You could not directly achieve the redirection simply adding the string parameter 'area' in the 'Login' action. Instead, you should pass the area value in the 'Login' process

•首先是当用户单击区域名称"时传递区域名称.登录按钮
•然后将区域名称传递给登录名用户完成用户凭据输入时的操作.
•最后一步是使用area值重定向到home索引成功登录后该区域的大小

• The first thing is to pass the area name when an user clicks the login button
• Then the area name should be passed to the login action when the user complete the input of the user credentials.
• The last step is to use the area value to redirect to the home index of the area after the successful login

关键是您应该在开始登录过程之前存储区域名称.

The key is that you should store the area name before start the login processs.

使用我为您提供的先前代码,您可以查看以下代码以了解更多详细信息.

With the previous codes I provided you, you could check the below codes for more details.

_LoginPartial.cshtml:

_LoginPartial.cshtml:

@using Microsoft.AspNetCore.Identity 
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
    <li class="nav-item">
        <a  class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="Index" title="Manage">Hello @User.Identity.Name!</a>
    </li>
    <li class="nav-item">
        <form method="post" asp-controller="Account" asp-action="Logout" asp-area="">
            <button type="submit" style="width: auto" class="nav-link btn btn-link py-0">
                Logout @User.Identity.Name
            </button>
            <input type="hidden" name="area" value="@ViewContext?.ActionDescriptor?.RouteValues["area"]" />
        </form>
    </li>
}
else
{
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="Register">Register</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-controller="Account" asp-action="Login" asp-area="" asp-route-areaName="@ViewContext?.ActionDescriptor?.RouteValues["area"]">Login</a>
    </li>
}
</ul>

帐户控制器:

 public async Task<IActionResult> Logout(string area) 
        {

            await _signInManager.SignOutAsync();
            return RedirectToAction("index", "home", new { area = area });
        }

        [HttpGet]
        public IActionResult Login(string areaName)
        {
            ViewBag.Area = areaName;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(LoginViewModel loginViewModel, string areaName)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.RememberMe, false);
                if (result.Succeeded)
                {
                    return RedirectToAction("index", "home",new { area = areaName });
                }
                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
            }
            return View(loginViewModel);
        }

Login.cshtml:

Login.cshtml:

<div class="row"> 
    <div class="col-md-4">
        <section>
            <form id="account" method="post" asp-controller="Account" asp-action="Login" asp-area="">
                <h4>Use a local account to log in.</h4>
                <hr />
                <div asp-validation-summary="All" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Email"></label>
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Password"></label>
                    <input asp-for="Password" class="form-control" />
                    <span asp-validation-for="Password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <div class="checkbox">
                        <label asp-for="RememberMe">
                            <input asp-for="RememberMe" />
                            @Html.DisplayNameFor(m => m.RememberMe)
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary" asp-route-areaName="@ViewBag.Area">Log in</button>
                </div>
                <div class="form-group">
                    <p>
                        <a id="forgot-password" asp-controller="Account"  asp-area="" asp-action="ForgotPassword">Forgot your password?</a>
                    </p>
                    <p>
                        <a asp-controller="Account" asp-action="Register" asp-area="">Register as a new user</a>
                    </p>
                </div>
            </form>
        </section>
    </div>
    
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

演示:

这篇关于成功登录后如何重定向到登录发生的同一区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆