0

登录后,用户不会返回到请求的 URL。

这是控制器

 public ActionResult LogIn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogIn(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (UserIsValid(model.Username, model.Password))
            {
                if (model.RememberMe == false)
                {

                }
                else
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        model.Username,
                        DateTime.Now,
                        DateTime.Now.AddDays(15),
                        false,
                        "user",
                        FormsAuthentication.FormsCookiePath);

                    string HashedCoockies = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashedCoockies);
                    Response.Cookies.Add(cookie);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password is incorrect.");
            }
        }

        return View(model);
    }

看法:

 @using (Html.BeginForm())
 {
                <label for="username">
                    Username:</label>
                <br />
                @Html.TextBoxFor(model => model.Username)
                <br />
                <br />
                <label for="password">
                    Password:</label>
                <p>
                    <a href="#">Forgot your password?</a></p>
                @Html.PasswordFor(model => model.Password)
                <br />
                <br />
                <div id="lower">
                    <br />
                    <br />
                    @Html.CheckBoxFor(model => model.RememberMe, new { style = "margin-left: 10px; margin-top: 7px; position: absolute;" })
                    <label for="check" id="keep" style="margin-left: 30px; margin-top: -14px;">
                        Keep me logged in</label>
                    <input type="submit" value="Login" class="round-button" style="float: right; margin-right: 20px;" />
                </div>
}

web.config

<authentication mode="Forms">
    <forms loginUrl="~/account/login" timeout="2880" />
</authentication>

ps 我的 login.cshtml 没有 _Layout.html

4

1 回答 1

1

returnUrl 可以为空。当 url 采用这种格式时,returnUrl 将有一个值

http://localhost:56457/Account/LogIn?returnUrl=whatever_return_url

当您尝试访问whatever_return_url(在访问之前需要授权)时,它会将您重定向到添加了returnUrl的登录页面(意味着您之前没有登录)

这意味着如果您的登录成功,它应该带您whatever_return_url。如果未指定 returnUrl(即:如果 returnUrl 为空),它应该在登录后将您带到“Home”控制器的“Index”操作。

http://localhost:56457/Home/Index

Check thoroughly your controller, action names and the validity of the URLs

于 2012-05-06T09:22:19.687 回答