4
A
回答
14
Cookies是創建以同樣的方式,因爲他們在普通的舊ASP.NET,你只需要訪問Response
。
public ActionResult Login(string username, string password, bool rememberMe)
{
// validate username/password
if (rememberMe)
{
HttpCookie cookie = new HttpCookie("RememberUsername", username);
Response.Cookies.Add(cookie);
}
return View();
}
但是,如果您正在使用窗體身份驗證,你可以讓你的FormsAuth票的Cookie持續性:
public ActionResult Login(string username, string password, bool rememberMe)
{
// validate username/password
FormsAuthentication.SetAuthCookie(username, rememberMe);
return View();
}
你可以這樣寫的餅乾:
public ActionResult Index()
{
var cookie = Request.Cookies["RememberUsername"];
var username = cookie == null ? string.Empty : cookie.Value; // if the cookie is not present, 'cookie' will be null. I set the 'username' variable to an empty string if its missing; otherwise i use the cookie value
// do what you wish with the cookie value
return View();
}
如果您爲使用表單認證並且用戶已登錄,您可以像這樣訪問其用戶名:
public ActionResult Index()
{
var username = User.Identity.IsAuthenticated ? User.Identity.Name : string.Empty;
// do what you wish with user name
return View();
}
可以解密和讀取票證的內容。如果需要,您甚至可以在票證中存儲少量的自定義數據。 See this article for more info.
相關問題
- 1. 在ASP.NET MVC中創建一個Cookie
- 2. 創建ASP.NET MVC 3形式
- 3. asp MVC 3 Cookie禁用
- 4. 創建具有MVC 3和jQueryUI的
- 5. 創建在MVC
- 6. 在Spring MVC控制器中創建的Cookie上設置http-only
- 7. 創建cookie Javascript
- 8. 創建Cookie
- 9. JS創建Cookie
- 10. 創建IP cookie
- 11. 創建在MVC
- 12. 如何創建cookie
- 13. MVC 3 RAZOR動態創建文本框
- 14. 創建成員資料MVC 3
- 15. 批量創建鑑於asp.net MVC 3
- 16. MVC 3 ViewModel創建方法模型
- 17. ASP.Net MVC 3剃刀創建報告
- 18. 創建自定義幫手Asp.net MVC 3
- 19. 創建MVC一個局部圖3
- 20. 從ID創建Cookie?
- 21. asp.net mvc 3全局變量沒有cookie
- 22. PHP不創建cookie
- 23. 創建Cookie通知
- 24. does session_name創建cookie
- 25. 創建Cookie導軌
- 26. Cookie創建錯誤
- 27. 監視cookie創建
- 28. 如何在MVC 3中的視圖的html塊中創建類?
- 29. 創建在CSS 3
- 30. MVC C#從jQuery和JSON創建的cookie中讀取值。stringify
嗨,我有兩件事要問.... 1。如果我們想查看視圖中存儲在cookie中的數據,那麼我們如何看待或調用它? –
和2.這個cookie和FormsAuthentication票據cookie之間的主要區別是什麼?或者兩者是相同的....? –
表單身份驗證是一種在ASP.NET中驗證用戶身份的方法。你不必使用它,但它被廣泛使用。 「票證」是表單驗證模塊在接收到每個請求時解密並驗證的加密cookie。如果票據cookie丟失或無效,用戶不會被視爲登錄。通常,您不關心FormsAuth Cookie的內容,只相信該模塊正在完成其工作(它很好)。我會通過如何閱讀cookies來改進我的答案。 – HackedByChinese