从会话重置登录后,Mvc returnurl始终为null [英] Mvc returnurl is always null after logging back in from session reset

查看:110
本文介绍了从会话重置登录后,Mvc returnurl始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有登录身份验证的MVC应用程序,并在登录一段时间后超时。所以我希望能够登录并返回到我所在的页面而不是去静态主页页。





每当我被踢出去,我的浏览器上都会收到帐户/登录?ReturnUrl = ...但我在Login ActionResult post方法中的调试器总是说我的returnUrl为null。我厌倦了以下方法,在此之前我尝试了其他方法,但没有一个有效。所以我真的被困了请求帮助:



我尝试过的事情:



AccountController.cs:



I'm working on an MVC app that has login authentication and times out after a while of being logging in. So I want to be able to log in and go back to the page I was rather than going to a static home page.


Every time I get kicked out, I do get a "Account/Login?ReturnUrl = ... " on my browser, but my debugger inside Login ActionResult post method always says my returnUrl is null. I tired the following method, before this I triedo other methods too but none have worked. So I'm really stuck please help:

What I have tried:

"AccountController.cs" :

public sealed class AccountController : BaseController
    {
        //Login prompt
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login() {

            return View();

        }

        //Login processing(authentication)
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        
        public ActionResult Login( LoginViewModel model, string returnUrl) {
            if ( ModelState.IsValid ) {
                var authenticated = AuthenticationProvider.Authenticate( model.UserName, model.Password, HttpContext );
                
                if( authenticated ) {
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        ViewBag.ReturnUrl = returnUrl;
                        return Redirect(returnUrl);
                    }
                  
                }

                ModelState.AddModelError( "", "Invalid username or password." );
            }

            return View( model );
        }







Loging.cshtml表格:




"Loging.cshtml" form:

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { ReturnUrl = Request.QueryString["ReturnUrl"] }))
    { .... 





web.config:





"web.config":

<modules>
     <remove name="FormsAuthentication" />
     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
   </modules>

推荐答案

你对HttpGet中的ReturnUrl参数没有任何意义。为了在HttpPost中提供它,您需要将其存储在表单中。



修改LoginViewModel类以添加ReturnUrl属性



You're going nothing with the ReturnUrl param in the HttpGet. In order for that to be available in the HttpPost you'll need to store it in the form.

Amend the LoginViewModel class to add a ReturnUrl property

public string ReturnUrl { get; set; }





捕获HttpGet方法中的url并确保它包含在表格中





Capture the url in the HttpGet method and ensure it is included in the form

public ActionResult Login(string returnUrl)
{
    LoginViewModel model = new LoginViewModel();
    model.ReturnUrl = returnUrl;

    return View(model);
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    return View(model);
}







@model LoginViewModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.ReturnUrl)

    // rest of form

    <input type="submit" value="Login"/>
}


这篇关于从会话重置登录后,Mvc returnurl始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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