【发布时间】:2010-09-26 07:17:20
【问题描述】:
刚刚花了 3 天时间探索会员资格、校长、身份和其他好东西..但仍有一些不清楚的地方。 为什么最好在会话中使用简单存储最小化登录用户对象的方法? 它可以保存角色、权限和其他自定义属性。
为了实现与我会做的 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 内容、移动它等等。
那么,还有人同意吗?
【问题讨论】:
标签: asp.net-mvc session membership