Create ProfileCommon Object in Asp.Net Membership

0

I need to create a "profile object" in Asp.net Membership.

My Web.config :

<profile defaultProvider="AspNetSqlProfileProvider">
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" applicationName="/" connectionStringName="labPuc" />
  </providers>
  <properties>
    <add name="UserId" type="System.String" />
    <add name="Nome" type="System.String" />
    <add name="Email" type="System.String" />
  </properties>
</profile>

How would you generate this profile?

ProfileCommon profile = Profile.GetProfile(usuario);
    
asked by anonymous 20.09.2014 / 03:10

1 answer

2

Before it's important to say that Profile and Membership are two completely different things, so much so that Profile has its own library ( System.Web.Profile ).

This Profile object is dynamic, so you defined it the way you wanted in your Web.config file containing:

  • UserId
  • Nome
  • Email

It's important to say that this object must be fully assembled by your application, especially for the attributes defined in your Web.config .

For the logged-in user, this dynamically created object is accessible through the HttpContext.Current.Profile property.

ProfileCommon does not exist implemented for projects from the .NET Framework 4.0. Microsoft at the time placed it discreetly in a migration manual (see "Converting Profile Object Code") . You can implement your own ProfileCommon class, if you want, that will work as an envelope for the ProfileBase class, something like this:

using System.Web;
using System.Web.Profile;

namespace MeuProjeto.Infrastructure
{
    public class ProfileCommon : ProfileBase
    {

        public ProfileUserData UserData
        { 
            get { return (ProfileUserData) GetPropertyValue("UserData"); }       
        } 

        public static ProfileCommon GetProfile() 
        { 
            return (ProfileCommon) HttpContext.Current.Profile; 
        } 

        public static ProfileCommon GetProfile(string userName) 
        { 
            return (ProfileCommon) Create(userName); 
        }  
    }

    // Aqui coloquei mais umas coisas, como um exemplo de como posso
    // estender esse objeto com coisas mais complexas, mas não que seja 
    // realmente necessário pra resposta.
    [Serializable]
    public class ProfileUserData
    {
        public string Endereco { get; set; }
        public string Numero { get; set; }
        public string Complemento { get; set; }
    }
}

Notice that using this:

ProfileCommon profile = Profile.GetProfile(usuario);

ProfileCommon will come empty, because the Create of ProfileBase method will create the object dynamically with the basics of basics, what can be seen here .

So, to fill in your object correctly, you would still have to change your envelope to bring the rest of the information:

public static ProfileCommon GetProfile(string userName) 
{ 
    var profileCommon = (ProfileCommon) Create(userName);
    profileCommon.UserID = //Coloque aqui o UserId
    profileCommon.Nome = // Coloque aqui o nome
    profileCommon.Email = // Coloque aqui o e-mail
    return profileCommon;
}
    
20.09.2014 / 19:32