Copied to clipboard

Flag this post as spam?

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


  • Srdjan Milic 5 posts 76 karma points
    Jul 28, 2016 @ 11:14
    Srdjan Milic
    0

    Forms Authentication - Add additional data in cookie

    Hi,

    By authenticating member to a umbraco site, SetAuthCookie must be used in order to do this:

    FormsAuthentication.SetAuthCookie(username, false)

    By going back the member to the site, this cookie will be read and will be used for Authentication procedure in order to be authenticated as a member of umbraco site.

    Is there any way please to add more data in that authentication cookie, for example email:

    FormsAuthentication.SetAuthCookie(username + "|" + email, false)

    but not to break authentication way of umbraco that the member be authenticated and authorized to umbraco as a member, and how to do that?

    Cheers

  • Srdjan Milic 5 posts 76 karma points
    Jul 28, 2016 @ 12:02
    Srdjan Milic
    1

    Hello,

    I have found the way, there is needed that Authentication cookie be manually created instead of SetAuthCookie:

        public static void Authenticate(string username, int memberGroupId)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
                                                                             username, 
                                                                             DateTime.Now, 
                                                                             DateTime.Now.AddMinutes(FormsAuthentication.Timeout.Minutes), 
                                                                             false, 
                                                                             memberGroupId.ToString());
    
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
            cookie.Value = FormsAuthentication.Encrypt(ticket);
    
            HttpContext.Current.Response.SetCookie(cookie);
        }
    

    In this case, memberGroupId is stored in UserData property of FormsAuthenticationTicket object encrypted and sent to member.

    In order to get memberGroupId from the cookie:

        public static Models.DocumentTypes.Core.MemberGroup CurrentMemberGroup
        {
            get
            {
                Models.DocumentTypes.Core.MemberGroup memberGroup = null;
    
                HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    
                if (cookie != null)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
    
                    if (ticket != null)
                    {
                        string memberGroupIdStr = ticket.UserData;
    
                        if (!string.IsNullOrEmpty(memberGroupIdStr))
                        {
                            int memberGroupId = 0;
    
                            if (int.TryParse(memberGroupIdStr, out memberGroupId) && memberGroupId > 0)
                            {
                                UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
                                memberGroup = helper.TypedContent(memberGroupId) as Models.DocumentTypes.Core.MemberGroup;
                            }
                        }
                    }
                }
    
                return memberGroup;
            }
        }
    

    The point of this task is that after entering member's username and password, the member must choose a Member group defined in Content tree of Umbraco and Authenticated to the site.

    This was challenge because the chosen MemberGroup cannot be stored in Session (Session - InProc), after restarting server, session is emtpy but member exists as authenticated on the site.

    This could help, for similar tasks that someone gets.

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft