过去3天只花了一些时间来探索会员资格,iprincipal,身份和其他好东西..但还有一些事情尚不清楚。 为什么最好使用简单存储最小化登录用户对象的会话? 它可以保存角色,权限和其他自定义属性。
实现同样的事情asp.net形式auth方式我会做:
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内容,移动它等等。
那么,其他人是否同意?
答案 0 :(得分:1)
默认的InProc会话状态不持久,只要您的应用程序池回收,它就会“消失”。
SqlStore会话是持久的,但是您的数据库服务器上还有额外的负载。
Cookie是目前网站的最佳选择,可能不会长时间改变。 Cookie是相对安全的,只要你加密cookie内容,默认情况下.net就可以了。你应该没问题。
注意:.Net中存在一个关于其默认加密方法的严重安全漏洞,所以当我说安全时,我的意思是安全的,可能会再次出现。
http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx