顯示具有 FormsAuthentication 標籤的文章。 顯示所有文章
顯示具有 FormsAuthentication 標籤的文章。 顯示所有文章

2016年12月28日 星期三

[ASP.NET MVC] 登入後,自動導向使用者原本Request的Url

練習筆記...


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

[ASP.NET MVC] Cookie與FormsAuthentication表單驗證自動登入

練習筆記...


[寫入Cookie]

寫法一:
HttpCookie cookie = new HttpCookie("UserData");
cookie.Value = JsonConvert.SerializeObject(userData);
cookie.Expires = DateTime.Now.AddDays(1d);

Response.Cookies.Add(cookie);



寫法二:
HttpCookie cookie = new HttpCookie("UserData")
{
       Value = JsonConvert.SerializeObject(userData),
       Expires = DateTime.Now.AddDays(1d),
};
Response.Cookies.Add(cookie); 


[取得Cookie]
HttpCookie authCookie = Request.Cookies["UserData”];


[FormsAuthentication寫入Cookie] 

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
      version: 1,
      name: data.Account,
      issueDate: DateTime.Now,
      expiration: DateTime.Now.AddDays(1),
      isPersistent: false,
      userData: JsonConvert.SerializeObject(userData),
      cookiePath: FormsAuthentication.FormsCookiePath);

var encryptTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket);
cookie.Expires = ticket.Expiration;
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);

※ Cookie name 一定要用FormsAuthentication的CookieName => FormsAuthentication.FormsCookieName


[取得FormsAuthentication寫入的Cookie]

HttpCookie cookie = GetCookie(FormsAuthentication.FormsCookieName);



Ref:
1. 寫入 Authentication Cookie 的 FormsAuthenticationTicket.UserData
2. session與cookies與FormsAuthentication自動登入機制
3. ASP.net MVC整合FormsAuthentication表單驗證登入