Copied to clipboard

Flag this post as spam?

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


  • Rich Green 2246 posts 4008 karma points
    Nov 22, 2010 @ 10:43
    Rich Green
    0

    Sync user and member login

    Hi,

    When an Umbraco user logs in I would also like them to be authenticated as a member

    Pseudo code:

    //On successful umbraco login (need to hook into this event)
    
    string UmbracoUserName= //Get username of umbraco user, which will be same as member name
    //create the authentication ticket System.Web.Security.FormsAuthenticationTicket authTicket = new System.Web.Security.FormsAuthenticationTicket(1, UmbracoUserName, DateTime.Now, DateTime.Now.AddMinutes(60), false); // encrypt the ticket string encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(authTicket); // create a cookie and add the encrypted ticket to the cookie as data HttpCookie authCookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, encryptedTicket); // add the cookie to the cookies collection returned to the user's browser Response.Cookies.Add(authCookie);

    I'm integrating an existing .net site into a new custom section, umbraco users need access to the Umbraco content and also certain parts of the new custom section which is set by membership roles.

    Appreciate any help

    Thanks

    Rich

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Nov 22, 2010 @ 14:18
    Matt Brailsford
    0

    Hey Rich,

    I'd probably extend the Umbraco Users membership provider so that when a user successfully logs in, you then issue the same command to the Members membership provider aswell.

    I'm not sure if it would work, but you could also try using one membership provider for both Users and Members (in the web.config).

    Matt

  • Eduardo 106 posts 130 karma points
    Nov 22, 2010 @ 14:31
    Eduardo
    0

    Hi Rich,

    I did this before through a custom profile

    1. Create a new Library Class project with visual studio

    Create an interface:

    namespace yournamespace
    {
        ///
        /// Summary description for IFromsAuthentication
        ///
        public interface IFormsAuthenticationService
        {
            void SignIn(string userName, bool createPersistentCookie);
            void SignOut();
        }
    }

     

    2. Create a class which inherits from the previous interface:

    namespace yournamespace
    {
        ///
        /// Summary description for FormsAuthentication
        ///
        public class FormsAuthenticationService : IFormsAuthenticationService
        {
            ///
            ///
            ///
            ///
            ///
            public void SignIn(string userName, bool createPersistentCookie)
            {
                if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

                System.Web.Security.FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
            }

            ///
            ///
            ///
            public void SignOut()
            {
                System.Web.Security.FormsAuthentication.SignOut();
            }
        }
    }


    3. Create a new profile, for example:

     public class MemberProfile : ProfileBase
        {
            public MemberProfile()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            [SettingsAllowAnonymous(false)]
    // Back End User of the Member we want to log in
            public string User
            {
                get
                {
                    var o = base.GetPropertyValue("user");
                    if (o == DBNull.Value)
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    base.SetPropertyValue("nombre", value);
                }
            }
        }
    }

    4. Run this function to log in the member just before user is authenticated.

    This authenticate the member through ASP.NET provider:

    public void LoginMember()
    {

    var usr = Member.GetMemberFromLoginName("login");

    if (usr != null)
    {
    if (System.Web.Security.Membership.ValidateUser(usr.Value, usr.Password))
    {
    BeComputed.IFormsAuthenticationService FormsService = new BeComputed.FormsAuthenticationService();
    FormsService.SignIn(usr.LoginName, true);
    }
    }
    }

     

    Remember to inherit your DLL in the member provider section in Web Config.

    HTH.

    Sincere regards,
    Eduardo Macho

Please Sign in or register to post replies

Write your reply to:

Draft