刚刚过去的3天,探索会员资格,iprincipal,身份和其他goodies ..但有些东西仍然不清楚。 为什么最好使用这个煽动的简单的存储会话中最小化登录用户对象?它可以保存角色,权限和其他自定义属性。ASP.NET MVC内置成员vs会话
来实现同样的事情asp.net表单身份验证方式,我会做:
protected void Application_AuthenticateRequest()
{
HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
if (cookie == null)
return;
bool isPersistent;
int webuserid = GetUserId(cookie, out isPersistent);
//Lets see if the user exists
var webUserRepository = Kernel.Get<IWebUserRepository>();
try
{
WebUser current = webUserRepository.GetById(webuserid);
//Refresh the cookie
var formsAuth = Kernel.Get<IFormsAuthService>();
Response.Cookies.Add(formsAuth.GetAuthCookie(current, isPersistent));
Context.User = current;
}
catch (Exception ex)
{
//TODO: Logging
RemoveAuthCookieAndRedirectToDefaultPage();
}
}
private int GetUserId(HttpCookie cookie, out bool isPersistent)
{
try
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
isPersistent = ticket.IsPersistent;
return int.Parse(ticket.UserData);
}
catch (Exception ex)
{
//TODO: Logging
RemoveAuthCookieAndRedirectToDefaultPage();
isPersistent = false;
return -1;
}
}
所以我需要查询数据库上的每个验证的请求,当使用会话我会做只有当用户登录时,我知道您可以将角色和其他用户数据存储在票据Cookie中,但我认为它不安全,因为攻击者可以修改cookie内容,将其移动并更多。
所以,其他人同意?
值得指出的有替代品以上也是如此,如果你快乐流浪因循守旧一点。 Mongo/Memcached对于这类事情可以很好地利用proc商店。 – Nik 2010-09-25 23:55:22
谢谢,但考虑一下上面的场景,是否有必要在每次获取用户obj并将其分配给上下文的请求中继续敲击数据库? – TomerMiz 2010-09-26 07:25:49
嗨,有谁可以回应? – TomerMiz 2010-09-26 21:12:55