簡體   English   中英

如何在 session 上獲取以前的 URL 在 ASP.NET MVC 中過期

[英]How to get previous URL on session expire in ASP.NET MVC

我試圖在空的 session 上獲取以前的 url 例如,如果用戶通過鍵入 url 直接打開網頁,例如 xyz.com/dashboard/ 然后因為他沒有登錄並且 session 是空的所以他將被重定向到 xyz.com/ login/ 他將在其中輸入他的憑據並自動重定向到 xyz.com/dashboard/

謝謝

將用戶重定向到xyz.com/login?next=<the url user accessed> ,然后在成功登錄后將用戶重定向到next查詢字符串值中指定的 URL。

為了補充@Ankur 的回答,我包括 MVC 和 Angular JS 代碼以實現所需的行為。

賬戶控制器.cs

        [HttpGet]
        [Route("login")]
        public ActionResult Login(string returnUrl)
        {
            if (Request.IsAuthenticated)
                return RedirectToAction("Home", "Dashboard");

            return View(new AccountLoginModel() { ReturnUrl = returnUrl });
        }

        [HttpGet]
        public ActionResult Login()
        {

            if (Request.IsAuthenticated)
                return RedirectToAction("Home", "Dashboard");

            return View(new AccountLoginModel() { ReturnUrl = string.Empty });
        }

登錄.chtml

<div data-ng-controller="AccountLoginViewModel" ng-init="returnUrl = '@Model.ReturnUrl'" class="centered">

... HTML code here

<button type="button" class="btn" ng-click="login()">Login</button>

</div>

AccountLoginViewModel.js

$scope.login = function () {
            viewModelHelper.apiPost('api/account/login', $scope.accountModel,
                function (result) {

                    if ($scope.returnUrl != '' && $scope.returnUrl.length > 1) {
                        window.location.href = rootPath + $scope.returnUrl.substring(1);
                    }
                    else {
                        window.location.href = rootPath + "dashboard/home";
                    }
                });
        }
  }

你可以試試

[Authorize]

在 controller 中的每個動作之前

[Authorize]
[HttpGet]
public ActionResult Dashboard()
{
}

在 Web.Config 配置中添加如下代碼

<authentication mode="Forms">
    <forms defaultUrl="/Home/Profile" loginUrl="/Home/Index" slidingExpiration="true" timeout="2880"></forms>
</authentication>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM