我有一個MVC3應用程序,我想給用戶設置偏好的能力,將在用戶登錄時啓用。我怎麼能存儲在一個MVC3用戶喜好
我真的沒有任何想法從哪裏開始,並真正感謝被指向正確的方向。我曾嘗試對會員階層進行一些更改,但現在我認爲這可能不是解決問題的最佳方式。
我有一個MVC3應用程序,我想給用戶設置偏好的能力,將在用戶登錄時啓用。我怎麼能存儲在一個MVC3用戶喜好
我真的沒有任何想法從哪裏開始,並真正感謝被指向正確的方向。我曾嘗試對會員階層進行一些更改,但現在我認爲這可能不是解決問題的最佳方式。
您可以在數據庫中完成此操作(聽起來您至少可以使用開箱即用的成員資格提供程序),一次唯一地標識用戶。在這種情況下,您可能需要實施自己的會員供應商。
你必須做一點工作才能開始實施你自己的提供者。如果這是你唯一的要求,你也許可以通過編寫自己的類,在您選擇
public static class UserSettings
{
public static string GetSettings(IPrincipal user)
{
if(user.Identity.IsAuthenticated)
{
// dip into database using user.Identity.Name property
return "string with user settings";
// this also assumes user.Identity.Name is uniquely able
// to identify a user in your database!
}
return string.Empty;
}
}
或者,如果信息是完全微不足道的,也許你可以實現一個cookie的格式返回設置,以避免它用戶設置的表示。這當然,帶有使用cookie的所有注意事項,但你可避免在數據庫中存儲的信息
任何地方你有一個HttpContext
你可以抓住的設置值,像這樣:
if(HttpContext.Current != null)
{
string userSettings = HttpRequest.Current.Request.Cookies["NameOfCookie"];
}
在您的數據庫中創建一個新表。
嗯,我真的想我怎麼可能從一個頁面保留用戶的喜好網頁我認爲這是對我不好做數據庫查詢每次我。向用戶展示一個頁面如果區域只有一些偏好,那麼人們通常會將這些內容存儲在Cookie中? – Grace 2011-06-09 13:06:15
您可以在會話期間始終保持數據庫返回的數據在內存中。 – 2011-06-14 06:51:21
你可以使用FormsAuthentication cookie存儲您的用戶信息並避免始終訪問數據庫。該cookie被加密,並且您存儲的任何信息與用戶會話本身一樣安全。 Cookie的唯一問題是它們的最大尺寸爲4K,因此,如果您的用戶信息很大,那麼您可能會遇到問題。當我使用cookie方法時,我將用戶數據存儲爲JSON,然後在每個頁面請求上反序列化該JSON。這裏是我的登錄控制器邏輯(我使用SimpleMembership但方法是一樣的:
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, model.RememberMe))
{
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null)
{
var user = _userLogic.GetItem(model.UserName);
if (user != null && user.IsActive)
{
var newAuthTicket = new FormsAuthenticationTicket(authTicket.Version, authTicket.Name, authTicket.IssueDate, authTicket.Expiration, authTicket.IsPersistent, JsonConvert.SerializeObject(user));
var newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newAuthTicket))
{
Expires = authCookie.Expires
};
Response.Cookies.Add(newCookie);
return RedirectToLocal(returnUrl);
}
WebSecurity.Logout();
ModelState.AddModelError("UserName", "This account has been deactivated.");
return View(model);
}
}
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
通知的newAuthTicket創作以及用戶實例傳遞到它作爲一個JSON之後,所有我需要做的就是在我的基本控制器的OnAuthorization方法desirialize此用戶對象:
protected override void OnAuthorization(AuthorizationContext filterContext)
{
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null)
{
var principal = new CustomPrincipal(HttpContext.User.Identity)
{
CurrentUserInfo = JsonConvert.DeserializeObject<User>(authTicket.UserData)
};
HttpContext.User = principal;
AppUser = principal.CurrentUserInfo;
ViewBag.AppUser = AppUser;
}
}
base.OnAuthorization(filterContext);
}
是的,我正在使用Microsoft.Samples.ServiceHosting.AspProviders和Azure表存儲。 – Grace 2011-06-09 13:08:33
使用MVC3的cookies有多可靠?我是否正確地說,會員提供商在cookie中存儲身份驗證信息? – Grace 2011-06-09 13:14:37
@Grace餅乾不可靠。它們可以由客戶端(用戶)編輯。你可以對它進行加密,但是如果你的信息*完全是微不足道的,那很難證明加密的合理性。也許這裏採用的方法是「如果cookie值不存在(或不能被分析),使用默認設置」。是的,開箱即用,MVC3使用認證cookie。 – 2011-06-09 13:17:38