Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Collin 42 posts 62 karma points
    Nov 22, 2011 @ 11:27
    Collin
    0

    Getting the profile for a member that isn't logged in

    For one of our websites we have some custom code to check if a member has a valid login. Currently we use the method Member.GetMemberFromEmail(); in our asp:LoginView to get the member and it's profile properties. However, this method is marked as obsolete and says I should use Membership.FindUsersByEmail();

    As this returns an MembershipUserCollection I do not know how to go from that to retrieving a property of the members profile (which with the GetMemberFromEmail method I could use the method .getproperty on the Member instance it returns).

    Any tips?

  • Collin 42 posts 62 karma points
    Nov 22, 2011 @ 11:32
    Collin
    0

    I forgot to mention that I'm doing this in my custom OnLoggingIn event handler of the asp:LoginView. So I'm fetching the member and it's profile before (s)he has logged in. This means I do not have access to HttpContext.Current.Profile.

  • Rodion Novoselov 694 posts 859 karma points
    Nov 22, 2011 @ 13:15
    Rodion Novoselov
    0

    Hi, Collin. What if to try to find a user profile with some static method of the ProfileManager class (e.g. ProfileManager.FindProfilesByUserName etc)?

  • Collin 42 posts 62 karma points
    Nov 22, 2011 @ 13:30
    Collin
    0

    Yes I can get profiles by using the following piece of code ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.All, loginControl.UserName);

    However I eventually get a ProfileInfo object with some basic properties/methods. But nothing obvious with which I can get the properties.

  • Ian Robinson 79 posts 143 karma points
    Nov 23, 2011 @ 11:28
    Ian Robinson
    0

    Hi Colin,

    Once you have the profile, I think you should be able to get the property by using the dot notation, e.g. Profile.PostalCode or Profile.Firstname.

    For this to work though, the properties need to be defined in your web.config Profile Properties section.

    http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

     


     

     

  • Rodion Novoselov 694 posts 859 karma points
    Nov 23, 2011 @ 13:17
    Rodion Novoselov
    0

    I've found how it's possible to acces a profile for an arbitrary member:

    var profile = (ProfileCommon)ProfileCommon.Create("some-member-name");

    Keep in mind that the "<profile>" section in the web.config file should be properly configured before.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 23, 2011 @ 13:54
    Tom Fulton
    1

    Hi,

    I always use the technique described here to create a custom MemberProfile class, then I can access like:

    MemberProfile mp = MemberProfile.GetUserProfile("username");
    txtFirstName.Text = mp.FirstName;
    txtLastName.Text = mp.LastName;

    (MemberProfile is the custom class)

    Hope this helps,
    Tom

  • Rodion Novoselov 694 posts 859 karma points
    Nov 23, 2011 @ 14:35
    Rodion Novoselov
    0

    Actually, you can easily skip the step with creating a custom class as far as you're going to use a profile from any asp.net stuff compiled on-demand, since it's anyway generated by asp.net from the list of profile properties listed in the web.config file.

    Frankly speaking I don't see a good reason to work with member type properties in umbraco through asp.net profile API. The main drawback would be that you will have to repeat property defenitions in the backoffice and the web.config (and perhaps in your custom profile base class as well).  I think that the only probable advantage could be perhaps reusing of some existing components that were created to work with user profiles.

  • Collin 42 posts 62 karma points
    Nov 28, 2011 @ 12:00
    Collin
    0

    The reason I'm working with profile properties to store information about logins is that some information could be needed months later (the security checks are quite complex). And storing them in the profile provides a convenient place for this where the information is also editable by administrators of the umbraco site. Another advantage is that this also enables us to display notifications/reminders about credentials after the user has logged in.

    I've tried several of the suggestions here but none work for me. A large problem for me is that I'm not familiar with how asp.net works in this aspect (or Umbraco) and code snippets do not give me the needed insight so that I can correctly implement it. I do appreciate the help, but I need to be guided through this from the beginning to implement it correctly.

    Currently I'm just more confused than when I started...

  • Collin 42 posts 62 karma points
    Nov 29, 2011 @ 14:13
    Collin
    0

    Just stumbled on the solution while working on something completely different.

    You can get a user if you do the following if you have an ID:

    Member member = new Member(userID, true);

    Now this will create a Member object even if this user is not present in the database. So it is your repsonsibility to check if this object represents something that is in the database. Checking if this user has an email address would suffice for most cases.

    Now as I don't have an ID available when I'm handling the login I need to take a few extra steps.

                    using umbraco.cms.businesslogic.member;
                    using System.Web.Security;               

                    Member umbracoMember = null;
                    try
                    {
                        MembershipUser membershipUser = Membership.GetUser(loginControl.UserName);
                        if (membershipUser != null)
                        {
                            int userID = (int)membershipUser.ProviderUserKey;
                            umbracoMember = new Member(userID, true);
                        }
                        else
                        {
                            //Your own custom code for handling a non existing user.
                            e.Cancel = true; //This btw cancels the login for an asp:LoginView
                            return;
                        }
                    }
                    catch
                    {
                        //Do your own custom error handling. See the following page for the exceptions: http://msdn.microsoft.com/en-us/library/40w5063z.aspx
                    }

    The key pieces of code are the following:

    MembershipUser membershipUser = Membership.GetUser(loginControl.UserName);
    int userID = (int)membershipUser.ProviderUserKey;
    umbracoMember = new Member(userID, false);

    Using Membership.GetUser means I use the membership provider specified in web.config. Via this route I can get an ID. This ID is an Object, but as we are working with Umbraco we know this should be an int object. So a simple cast will suffice to make it usable for the Member constructor.

    And now that we have an Umbraco Member object we instantly get access to all the properties we have defined on the member type and in the web.config. Happy days.

    This shows it is really easy to get a profile for a member that isn't logged in. You just need to know what route you need to take through the API. Which would be very obvious if the Umbraco documentation would mention this.

    Btw, does anyone know what the option noSetup does for the Member constructor? I couldn't find a gooed explanation in the documentation or code documentation/comments.

Please Sign in or register to post replies

Write your reply to:

Draft