我有同样的问题,我通过使用身份验证票将所需数据传递给身份对象来解决它。
然后不需要将对象注入成员提供程序。
在我的身份验证代码中,我有
[NonAction]
private void SetAuthTicket(Member member, bool isPersistent)
{
HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
FormsAuthentication.SetAuthCookie(member.Email, isPersistent);
string userData = "|" + member.ID + "|" + member.Firstname + "|" + member.Lastname + member.Culture;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, member.Email, DateTime.UtcNow, DateTime.UtcNow.AddDays(7), isPersistent, userData);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(cookie);
}
在 Global.asax
void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{
IPrincipal user = HttpContext.Current.User;
if (user.Identity.IsAuthenticated && user.Identity.AuthenticationType == "Forms")
{
FormsIdentity identity = user.Identity as FormsIdentity;
MyIdentity ai = new MyIdentity(identity.Ticket);
MyPrincipal p = new MyPrincipal(ai);
HttpContext.Current.User = p;
System.Threading.Thread.CurrentPrincipal = p;
if (!String.IsNullOrEmpty(ai.Culture))
{
CultureInfo ci = new CultureInfo(ai.Culture);
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
}
}
在 Identity 类中,我有 MemberName、Firstname 和 Lastname 属性,它们返回票证字符串的一部分。