User 驗証未通過,登入後,自動導向User原本Request的Url,三種作法:
1. 使用FormsAuthentication
2. 使用ViewBag
3. 使用讀寫屬性
1. 使用FormsAuthentication
FormsAuthentication.RedirectFromLoginPage(username, false);
2.使用ViewBag
@using (Html.BeginForm("index", "login", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { autocomplete = "off" }))
{
@Html.TextBoxFor(m => m.Account)
@Html.TextBoxFor(m => m.Password)
<button class="btn btn-lg btn-primary btn-block" type="submit">登入</button>
}
[AllowAnonymous, HttpGet]
public ActionResult Index(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
3.使用讀寫屬性
public string ReturnUrl { get; set; }
@using (Html.BeginForm("index", "login", new { ReturnUrl = Model.ReturnUrl }, FormMethod.Post, new { autocomplete = "off" }))
{
@Html.TextBoxFor(m => m.Account)
@Html.TextBoxFor(m => m.Password)
<button class="btn btn-lg btn-primary btn-block" type="submit">登入</button>
}
[HttpGet]
public ActionResult Index(string returnUrl)
{
LoginViewModel model = new LoginViewModel() { ReturnUrl = returnUrl };
return View(model);
}
HttpPost ActionResult
[HttpPost]
public ActionResult Index(LoginViewModel data, string returnUrl)
{
//Todo something
if (string.IsNullOrWhiteSpace(returnUrl))
//RedirectToAction default url or other.
else
return Redirect(returnUrl);
return View();
}
Ref:
1. ASP.net MVC整合FormsAuthentication表單驗證登入
https://dotblogs.com.tw/shadow/archive/2014/03/01/144191.aspx
2. Asp.net MVC 實作登入驗證(1)
http://marco.easyusing.com/2013/10/mvc-1-mvc-mvc-mvc5-build-4.html
沒有留言:
張貼留言