C# 如何获取最近通过身份验证的用户?
我正在使用MVC 3,我刚刚为FormsAuthenticationService实现了一个包装器 类似于下面的内容C# 如何获取最近通过身份验证的用户?,c#,security,asp.net-mvc-3,authentication,cookies,C#,Security,Asp.net Mvc 3,Authentication,Cookies,我正在使用MVC 3,我刚刚为FormsAuthenticationService实现了一个包装器 类似于下面的内容 public void SignIn(string username, bool createPersistantCookie) { if (string.IsNullOrEmpty(username)) throw new ArgumentException("Value Cannot be null or empty", "username");
public void SignIn(string username, bool createPersistantCookie)
{
if (string.IsNullOrEmpty(username))
throw new ArgumentException("Value Cannot be null or empty", "username");
FormsAuthentication.SetAuthCookie(username, createPersistantCookie);
}
我很不情愿地让它工作,但现在我不太确定如何获取我存储的信息
一旦用户进入我的系统,如果我需要从数据库中获取他们的用户id,我现在如何安全地检索这些信息?您实际上还没有在数据库中存储用户id。您编写的所有代码都是在用户计算机上存储一个身份验证cookie,要么作为会话cookie而不是持久性cookie,要么作为持久性cookie
当您的页面刷新时,它将自动获取cookie,对其进行解码,并填充您从User.Current属性访问的IPrincipal对象 您尚未在数据库中实际存储用户id。您编写的所有代码都是在用户计算机上存储一个身份验证cookie,要么作为会话cookie而不是持久性cookie,要么作为持久性cookie
当您的页面刷新时,它将自动获取cookie,对其进行解码,并填充您从User.Current属性访问的IPrincipal对象 根据提供的附加信息,您希望使用FormsAuthentication票证存储附加数据。为此,您需要首先创建自定义表单身份验证票证:
var authenticationTicket = FormsAuthentication.Decrypt(formsCookie.Value);
if (authenticationTicket.Expired)
return false;
存储数据
抓住当前的HttpContext,不要担心可测试性
var httpContext = HttpContext.Current;
确定票证何时到期:
var expires = isPersistent
? DateTime.Now.Add(FormsAuthentication.Timeout)
: NoPersistenceExpiryDate; // NoPersistenceExpiryDate = DateTime.MinValue
创建新的FormsAuthentication票证以保存自定义数据
var authenticationTicket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now,
DateTime.Now.Add(FormsAuthentication.Timeout),
isPersistent,
"My Custom Data String"); //Limit to about 1200 bytes max
创建您的HTTP cookie
new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authenticationTicket))
{
Path = FormsAuthentication.FormsCookiePath,
Domain = FormsAuthentication.CookieDomain,
Secure = FormsAuthentication.RequireSSL,
Expires = expires,
HttpOnly = true
};
最后添加到响应中
httpContext.Response.Cookies.Add(cookie);
检索数据
然后,您可以通过解析存储的身份验证票证来检索后续请求中的数据
再次,获取当前的HttpContext
var httpContext = HttpContext.Current
检查请求是否已在应用程序\u AuthenticateRequest或OnAuthorize中通过身份验证
if (!httpContext.Request.IsAuthenticated)
return false;
检查您是否有FormsAuthentication票证,并且该票证尚未过期:
var formsCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (formsCookie == null)
return false;
检索FormsAuthentication票证:
var authenticationTicket = FormsAuthentication.Decrypt(formsCookie.Value);
if (authenticationTicket.Expired)
return false;
最后检索您的数据:
var data = authenticationTicket.UserData;
根据提供的附加信息,您希望使用FormsAuthentication票证存储附加数据。为此,您需要首先创建自定义表单身份验证票证:
var authenticationTicket = FormsAuthentication.Decrypt(formsCookie.Value);
if (authenticationTicket.Expired)
return false;
存储数据
抓住当前的HttpContext,不要担心可测试性
var httpContext = HttpContext.Current;
确定票证何时到期:
var expires = isPersistent
? DateTime.Now.Add(FormsAuthentication.Timeout)
: NoPersistenceExpiryDate; // NoPersistenceExpiryDate = DateTime.MinValue
创建新的FormsAuthentication票证以保存自定义数据
var authenticationTicket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now,
DateTime.Now.Add(FormsAuthentication.Timeout),
isPersistent,
"My Custom Data String"); //Limit to about 1200 bytes max
创建您的HTTP cookie
new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authenticationTicket))
{
Path = FormsAuthentication.FormsCookiePath,
Domain = FormsAuthentication.CookieDomain,
Secure = FormsAuthentication.RequireSSL,
Expires = expires,
HttpOnly = true
};
最后添加到响应中
httpContext.Response.Cookies.Add(cookie);
检索数据
然后,您可以通过解析存储的身份验证票证来检索后续请求中的数据
再次,获取当前的HttpContext
var httpContext = HttpContext.Current
检查请求是否已在应用程序\u AuthenticateRequest或OnAuthorize中通过身份验证
if (!httpContext.Request.IsAuthenticated)
return false;
检查您是否有FormsAuthentication票证,并且该票证尚未过期:
var formsCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (formsCookie == null)
return false;
检索FormsAuthentication票证:
var authenticationTicket = FormsAuthentication.Decrypt(formsCookie.Value);
if (authenticationTicket.Expired)
return false;
最后检索您的数据:
var data = authenticationTicket.UserData;
您需要检索什么类型的信息?只是他们的用户名?如果是这样,则可以将其存储在表单cookie中,而无需访问数据库。请澄清。您需要检索什么类型的信息?只是他们的用户名?如果是这样,则可以将其存储在表单cookie中,而无需访问数据库。请澄清。很抱歉,我只是错误地解释了我的问题。我的意思是如何访问我刚刚存储在cookie中的内容。我计划最终在用户名之外存储一些东西,我只是想知道如何从cookie中访问它……很抱歉,我只是错误地解释了我的问题。我的意思是如何访问我刚刚存储在cookie中的内容。我计划最终在用户名之外存储一些东西,我只想知道如何从cookie中访问它。。。