我目前为我的MVC应用程序实现了一些“检查器”。
到目前为止,这就是我所拥有的,
GET
url来访问其他人的数据。cache
关于ASP MVC的最佳做法,我有几个问题。
这是我登录的实现:
[HttpGet]
[ActionName("login")]
public ActionResult login_load()
{
return View();
}
[HttpPost]
[ActionName("login")]
public ActionResult login_post(string uname,string pword)
{
using (EmployeeContext emp = new EmployeeContext())
{
//h student log = new student();
int success = emp.login.Where(x => x.username == uname && x.password == pword).Count();
if (success == 1)
{
int id = (from logs in emp.login
join rol in emp.roles on logs.role equals rol.id
where logs.username == uname
select logs.id).First();
FormsAuthentication.SetAuthCookie(uname, false);
HttpRuntime.Cache.Insert("id", id);
return RedirectToAction("Details", "Enrollment", new { id = id});
}
return View();
}
}
(我计划尽快实施H&S)
无论如何,到目前为止,这是我的担忧:
id
在缓存中存储诸如的内容吗?或者,如果我使用更好sessions
?假设我成功登录,并添加了以下代码行:
HttpRuntime.Cache.Insert("id", id);
是要编辑我以前的记录还是要添加另一个条目?我有来自Custom的这段代码RoleProvider
,HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);
并且我相信每次我问保护为的控制器时,它们都会被“解雇” [Authorize(Role="users")]
。那么,它是否要创建一个新条目或编辑上一个/现有条目?
cache
用户决定注销后立即删除/清除我的信息吗?我的角色提供者超时当前设置为20 minutes
我需要使用,id
因为除了用户名之外,它是我的唯一标识符,并且我使用它来比较用户尝试访问的任何ID。
我在想是否有可能编辑缓存并针对我的应用程序使用它。
出于安全方面的考虑,可以在缓存中存储诸如id之类的东西吗?还是最好使用会话?
在这种情况下,差别不大。但是,缓存不能分布在多个Web服务器上。会话状态可以通过更改文件的<sessionState>
部分来实现web.config
。因此,您通过使用缓存在应用程序的可伸缩性方面建立了固有的限制。
假设我成功登录,并添加了以下代码行:
HttpRuntime.Cache.Insert("id", id);
是要编辑我以前的记录还是要添加另一个条目?我从我的自定义角色提供程序HttpRuntime.Cache.Insert(cacheKey,Roles,null,DateTime.Now.AddMinutes(_cacheTimeoutInMinute),Cache.NoSlidingExpiration)中获得了这些代码; 并且我相信每次我问一个具有[Authorize(Role =“ users”)]保护的控制器时,它们都会被“解雇”。那么,它是否要创建一个新条目或编辑上一个/现有条目?
首先,您的代码存在一个重大缺陷。缓存旨在在站点上的所有用户之间共享。因此,当您插入一个值(例如)时HttpRuntime.Cache.Insert("id", id);
,所有用户都会看到它。如果您以后使用此值查找数据,则用户数据将始终是最后登录的用户的数据。
您可以通过在密钥中添加用户唯一的值来解决此问题。
var key = this.User.Identity.Name + "|Id";
HttpRuntime.Cache.Insert(key, id);
注意我在这里使用竖线字符作为分隔符。这是假设用户名不允许使用竖线字符(您还需要确保该字符)。
其次,使用适当的缓存模式意味着您将不必担心“ id”是否存在,因为您已经进行了该检查。使用缓存通常看起来像这样。
public static string GetUserID()
{
// Check whether the user is logged in
if (!HttpContext.Current.User.Identity.IsAuthenticated) {
return 0;
}
// Make a UNIQUE key that can be used for this scenario
var userName = HttpContext.Current.User.Identity.Name;
var key = userName + "|Id";
// Attempt to get the ID from the cache
var id = HttpRuntime.Cache[key];
// A null value indicates there was no value in the cache
if (id == null)
{
// No ID in the cache, look it up from the database
using (EmployeeContext emp = new EmployeeContext())
{
id = (from user in emp.login
where user.username = userName
select user.id).First();
}
// Store the ID from the database into the cache
HttpRuntime.Cache.Insert(key, id,
// No Dependencies
null,
// No absolute expiration (mimic the behavior of forms authentication)
System.Web.Caching.Cache.NoAbsoluteExpiration,
// Timeout 20 minutes after the last access
// (to mimic the behavior of forms authentication)
new TimeSpan(0, 20, 0),
// Setting to NotRemovable ensures that if the
// application pool restarts, you don't lose your cache
System.Web.Caching.CacheItemPriority.NotRemovable,
// No callback needed here
null);
}
return (string)id
}
当然,如果该值可在登录时将其直接插入到高速缓存中,则可以提高性能,但是在这种情况下,您需要确保使用相同的键。
在这种情况下,会话可能是一个更好的选择,但是无论哪种方式,您都应该利用此模式来仔细检查您是否有一个值,然后再将其返回给用户。
用户决定注销后,我是否应该担心删除/清除缓存?我的角色提供者超时当前设置为20分钟
如果您使用会话状态而不是缓存,这会容易得多。Session.Abandon()
用户注销后只需致电即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句