Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新自定义成员身份用户_C#_Asp.net Mvc_Entity Framework_Asp.net Membership - Fatal编程技术网

C# 更新自定义成员身份用户

C# 更新自定义成员身份用户,c#,asp.net-mvc,entity-framework,asp.net-membership,C#,Asp.net Mvc,Entity Framework,Asp.net Membership,我似乎在任何地方都找不到答案。我正在使用一个自定义的成员资格提供者和成员资格用户来处理我自己的表,这看起来太麻烦了。当更新用户记录时,我似乎也需要更新它的成员资格用户实例,否则在我依赖成员资格用户的数据时,它与更新的数据库不对应 我创建了自己的update membership user方法,因为现有方法只接受自己的membership user类: public static void UpdateAccountUser(AccountUser accountUser) {

我似乎在任何地方都找不到答案。我正在使用一个自定义的成员资格提供者和成员资格用户来处理我自己的表,这看起来太麻烦了。当更新用户记录时,我似乎也需要更新它的成员资格用户实例,否则在我依赖成员资格用户的数据时,它与更新的数据库不对应

我创建了自己的update membership user方法,因为现有方法只接受自己的membership user类:

public static void UpdateAccountUser(AccountUser accountUser)
    {
        // Custom MembershipUser
        ToMembershipUser user = new ToMembershipUser(
                    "AccountUserMembershipProvider",
                    accountUser.FirstName + " " + accountUser.LastName,
                    accountUser.ID,
                    accountUser.Email,
                    "",
                    "",
                    true,
                    false,
                    DateTime.Now,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue);

        // Fill additional properties
        user.ID = accountUser.ID;
        user.Email = accountUser.Email;
        user.FirstName = accountUser.FirstName;
        user.LastName = accountUser.LastName;
        user.Password = accountUser.Password;
        user.MediaID = accountUser.MediaID;
        user.Media = accountUser.Media;
        user.Identity = accountUser.Identity;
        user.CreatedAt = accountUser.CreatedAt;
        user.UpdatedAt = accountUser.UpdatedAt;

        UpdateCookie(user.Email);
    }
    private static void UpdateCookie(string email)
    {
        HttpCookie cookie = FormsAuthentication.GetAuthCookie(email, true);
        var ticket = FormsAuthentication.Decrypt(cookie.Value);

        // Store UserData inside the Forms Ticket with all the attributes
        // in sync with the web.config
        var newticket = new FormsAuthenticationTicket(ticket.Version,
                                                      ticket.Name,
                                                      ticket.IssueDate,
                                                      ticket.Expiration,
                                                      true, // always persistent
                                                      email,
                                                      ticket.CookiePath);

        // Encrypt the ticket and store it in the cookie
        cookie.Value = FormsAuthentication.Encrypt(newticket);
        cookie.Expires = newticket.Expiration.AddHours(24);
        HttpContext.Current.Response.Cookies.Set(cookie);
    }
现在很明显,这只是创建一个新实例,而不是更新现有实例,这不允许我在不注销和重新登录的情况下查看更新的详细信息。有什么想法吗

编辑 因此,我设法找到了一个其他人使用他们自己的自定义更新方法的示例,但他们似乎只是在更新数据库,而不是MembershipUser本身。我已经这么做了

我尝试只更新FormsAuthenticationCookie,然后在调用Membership.GetUser()时,我至少传递了一个更新的User.Identity.Name,但没有用,即使数据库已更新,仍然是旧数据。我已经没有主意了


这可能是昨天问题的重复

我最终发现MembershipUser没有更新,因为每次调用GetUser()时,所涉及的查询都使用FirstOrDefault()。结果表明,这会缓存结果,而不是检索新结果。通过将AsNoTracking()添加到查询本身,我现在可以获得更新的结果,甚至不必调用UpdateUser()