Create Object Profilecommon in Asp.Net Membership

Asked

Viewed 128 times

1

I need to create a "profile object" in Asp.net Membership (Asp.net Security Control).

Man 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);

1 answer

2


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

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

  • UserId
  • Nome
  • Email

It is important to say that this object must be fully assembled by its application, mainly regarding the attributes defined in its Web.config.

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

ProfileCommon does not exist implemented for projects from Framework . NET 4.0. Microsoft at the time placed this discreetly in their migration manual (see "Converting Profile Object Code"). You can implement your own class ProfileCommon, if you want, it will work as an envelope for the class ProfileBase, sort of 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 like this:

ProfileCommon profile = Profile.GetProfile(usuario);

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

Therefore, to fill 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;
}
  • public Static Profilecommon Getprofile(string username) { } How do I retrieve database data?

  • @Marconi How does your application work with the bank? This is what you need to know.

  • I logged in with Memberships authentication. I tried to recover my profile and nothing through Httpcontext.Current.Profile

  • Obviously, because the object is empty. You ride it.

  • Profilecommon so if I can assign values to the target Profilecommon I can get its value on any page?

  • I understand your gypsy logic. When I Request this profile from Getprofile should I request asp_Membership and assign the values to profileCommon? This will make my profile accessible to all pages?

  • @Marconi Exactly. Logic is not my hehehehe. The component works like this.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.