Gra*_*ace 5 asp.net-mvc asp.net-mvc-3
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果信息完全无关紧要,也许您可以实现用户设置的 cookie 表示。当然,这伴随着使用 cookie 的所有注意事项,但您可以避免将信息存储在数据库中
任何地方HttpContext你都可以获取设置值,如下所示:
if(HttpContext.Current != null)
{
string userSettings = HttpRequest.Current.Request.Cookies["NameOfCookie"];
}
Run Code Online (Sandbox Code Playgroud)