How do I manage profiles using SimpleMembership?

后端 未结 4 1697
情深已故
情深已故 2020-12-04 13:36

I have an ASP.NET MVC 4 site based off the internet template. I am using the SimpleMembership which i set up with that template.

I can modify the Users table which h

4条回答
  •  北海茫月
    2020-12-04 14:20

    if you look right around line 273 of the accountcontroller you'll find this line

    db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
    

    Looks like even OOTB they (MS) are doing just as you suggested and using EF to update.

    I too, am looking for the "correct" way of updating and accessing these properties.

    Edit:

    Here's my solution (I'm happy if someone says there's an OOTB way to do this).

    wrap UserProfile (the .net Entity from SimpleMembership) in a session class.

    public static class sessionHelpers {
         public static UserProfile userProfile
            {
                get
                {
                    if (HttpContext.Current.Session["userProfile"] != null)
                    {
                        return HttpContext.Current.Session["userProfile"] as UserProfile; 
                    }
                    else
                    {
                        using (UsersContext db = new UsersContext())
                        {
                            HttpContext.Current.Session["userInfo"] =
                            db.UserProfiles.Where(x => x.UserName == 
                                HttpContext.Current.User.Identity.Name).FirstOrDefault();
    
                       return db.UserProfiles.Where(x => x.UserName == 
                           HttpContext.Current.User.Identity.Name).FirstOrDefault();
                        }
                    }
                }
                set { HttpContext.Current.Session["userProfile"] = value; }
            }
    }
    

    From this you can access the profile table by doing

    string foo = sessionHelpers.userProfile.FIELDNAME;
    

    where sessionHelpers is my wrapper class. The if block just ensures that if it hasn't been set in the current session that accessing it will attempt to get it.

提交回复
热议问题