C# 为什么这个自定义身份用户上下文不能在MVC中持久化?

C# 为什么这个自定义身份用户上下文不能在MVC中持久化?,c#,asp.net-mvc,custom-membershipprovider,C#,Asp.net Mvc,Custom Membershipprovider,我创建了CustomPrincipal、CustomIdentity、CustomMembershipProvider等,并在用户登录时全部填充: public class CustomIdentity : IIdentity { private IIdentity _identity; // in the future maybe use a dictionary instead //private Dictionary<

我创建了CustomPrincipal、CustomIdentity、CustomMembershipProvider等,并在用户登录时全部填充:

    public class CustomIdentity : IIdentity
    {

        private IIdentity _identity;

        // in the future maybe use a dictionary instead
        //private Dictionary<string, object> _customValues

        private int _userId;
        private bool _IsAuthenticated;
        private string _name;
        private string _displayName;
        private string _role;


        private Website _currentProject;

        public Website CurrentProject
        {
            get { return _currentProject; }
            set { _currentProject = value; }
        }


        private string _userName;

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
   ...

其中一些是正确的吗?大部分都被注释掉了。

您必须在每次回发时替换用户,如本文所述:

受保护的无效应用程序\u PostAuthenticateRequest(对象发送方,事件参数e)
{
HttpCookie authCookie=Request.Cookies[FormsAuthentication.formscookeName];
if(authCookie!=null)
{
FormsAuthenticationTicket authTicket=FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer=新的JavaScriptSerializer();
if(authTicket.UserData==“OAuth”)返回;
CustomPrincipalSerializedModel SerializedModel=
反序列化(authTicket.UserData);
CustomPrincipal newUser=newcustomprincipal(authTicket.Name);
newUser.Id=serializeModel.Id;
newUser.FirstName=serializeModel.FirstName;
newUser.LastName=serializeModel.LastName;
HttpContext.Current.User=newUser;
} 
}

这会在每次回发时运行,因此每次都必须重建自定义标识。为方便起见,您可以在会话中持久化对象,而不是也从ticket或DB重新加载对象。

您必须在客户端浏览器上设置cookie。在cookie中,您将放置一些东西(可能是用户Id),从中可以再次填充
customIdentity
。现在在您的
Global.asax
中,对于
AuthenticationRequest
事件,您将检查cookie是否存在,如果存在,您将必须通过解密cookie的值从cookie中取出(以前存储的Id)某些内容,并填充您的
customIdentity
,并将其添加到当前线程上下文中

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
   HttpContext.Current.User = new GenericPrincipal(customIdentity, null);
}

很可能身份验证cookie中缺少此属性。您必须提供更多关于如何在cookie中持久化标识的信息。Thx,需要哪些其他详细信息?我的回答是对的,谢谢您的提示。但是,在重定向到相应页面时出现问题:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
      HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
      if (authCookie != null)
      {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        if (authTicket.UserData == "OAuth") return;
        CustomPrincipalSerializedModel serializeModel = 
          serializer.Deserialize<CustomPrincipalSerializedModel>(authTicket.UserData);
        CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
        newUser.Id = serializeModel.Id;
        newUser.FirstName = serializeModel.FirstName;
        newUser.LastName = serializeModel.LastName;
        HttpContext.Current.User = newUser;
      } 
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
   HttpContext.Current.User = new GenericPrincipal(customIdentity, null);
}