zoukankan      html  css  js  c++  java
  • MVC 登录后重定向回最初请求的 URL FormsAuthentication.RedirectFromLoginPage

      在传统的Asp.net webForm 中如果使用 Form身份验证。登录后重定向到最初请求的页面只需使用

    FormsAuthentication.RedirectFromLoginPage

      但在MVC中,MVC可不吃这一套。试了很多方法都不行。但最终还是搞定了。之所以标题中写到“FormsAuthentication.RedirectFromLoginPage”。是因为我一开始不停的在Google中找“FormsAuthentication.RedirectFromLoginPage”这个关键字想到找到这个要怎么样应用到MVC中。或者在MVC中有没有替代的写法。没有成功但受到了一些启发。

    因为没有登录用户如果不是直接访问登录页是会被重定向到登录页面。并且URL后面会跟上“ReturnUrl”参数记录是从哪个页面跳转过来的。

    那么我们可以这样操作

    1.在登录的“Action” 方法中接收“ReturnUrl”参数。

    2.在验证登录的“Action”方法中登录成功后,判断如果“ReturnUrl”不为空就跳转到“ReturnUrl”指向的页面。

    代码:

    1.实体:

    在登录的实体中增加了“ReturnUrl”参数用于接收登录前的页面地址

        public class LoginInfo
        {
            public string LoginName { get; set; }
    
            public string LoginPwd { get; set; }
    
            public string SecurityCode { get; set; }
    
            public string  ReturnUrl { get; set; }
        }

    2.登录页面的Action

            [AllowAnonymous]
            public ActionResult Login(string returnUrl)
            {
                ViewBag.returnUrl = returnUrl;
    
                return View();
            }

    3.登录页面视图

    辅助方法“@Html.Hidden("returnUrl")”会自动的生成“<input id="returnUrl" name="returnUrl" type="hidden">”HTML代码,并且绑定“ViewBag.returnUrl”属性。
    <form action="/User/Login" method="post">
        <input type="text" name="LoginName" />
        <input type="password" name="LoginPwd" />
        <input type="text" name="SecurityCode" />
        <img id="veri_code" src="@Url.Action("AuthenticationCode", new { r = new Random().NextDouble() })" width="65" height="23" class="identify_code" alt="验证码" />
        <a href="javascript:void(0);" onclick="refreshCode()">刷新</a>
        <input type="submit" value="登录" />
        @Html.Hidden("returnUrl")
    </form>

    4.执行登录验证的Action

            [HttpPost]
            [AllowAnonymous]
            public void Login(LoginInfo login)
            {
                if (login.SecurityCode != SessionUtil.AuthenticationCode)
                    throw new BusinessException("验证码错误");
    
                var user = UserService.Instance.Login(login.LoginName, login.LoginPwd);
                SessionUtil.Current = user;
    
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, user.Id.ToString(), DateTime.Now, DateTime.Now.AddYears(1),
                true, string.Empty, FormsAuthentication.FormsCookiePath);
    
                string ticString = FormsAuthentication.Encrypt(ticket);
    
                HttpCookie coo = new HttpCookie(FormsAuthentication.FormsCookieName, ticString);
    
                if (ticket.IsPersistent)
                {
                    coo.Expires = ticket.Expiration;
                }
                
                Response.Cookies.Add(coo);if (string.IsNullOrWhiteSpace(login.ReturnUrl))
                    Response.Redirect("/PublicAccountWater/Index");
                else
                    Response.Redirect(login.ReturnUrl);
            }
  • 相关阅读:
    路由器桥接是个什么玩法
    MAC使用小技巧之------用好mac电脑的10个必知的小技巧!
    学习笔记1--响应式网页+Bootstrap起步+全局CSS样式
    mysql运维必会的一些知识点整理
    面试小结1--填空题
    CSS技术实例1-使用CSS计数器实现数值计算小游戏实例页面
    编译8.0
    解决Windows 10 1809 使用管理员权限运行的程序无法浏览网络驱动器的问题
    android sdk
    酷卓教程 明明已经已经有了面具Magisk 确无法正常使用root权限
  • 原文地址:https://www.cnblogs.com/lookforFree/p/4129509.html
Copyright © 2011-2022 走看看