Copied to clipboard

Flag this post as spam?

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


  • Yanick Van Barneveld 27 posts 148 karma points
    Sep 14, 2016 @ 08:16
    Yanick Van Barneveld
    0

    Extend HttpContent.Current.User

    Hi,

    I am trying to extend the user identity in front office of Umbraco with the following tutorial.

    This makes it possible to do the following thing in Razor: "@((User as CustomPrincipal).FirstName)", so you can access custom fields in the front office after you are logged in with a custom form, which works perfectly

    But when I am logged in in the front office with a custom login and now want to login in the back office with the right credentials it redirects you to a blank page and you are not logged in, so you can't edit any page. It does not give you any error or something.

    Someone knows what the problem is?

    Kind regards,

    Yanick

  • Jan A 63 posts 268 karma points
    Sep 19, 2016 @ 08:12
    Jan A
    0

    Do you extend the member or User? In umbraco you have a member that is a frontend user, and Users are the backend users.

    It seems like you trying to add fields to the frontend member. I would suggest you do this by going to members in backend, add a property to the memberType, or add a new memberType if the properties should only apply to some.

    Read more about member types here: https://our.umbraco.org/documentation/getting-started/data/members/

  • Yanick Van Barneveld 27 posts 148 karma points
    Sep 19, 2016 @ 11:01
    Yanick Van Barneveld
    0

    Hi Jan,

    Yes, I want to extend the Umbraco member. But I am logged in using a Active Directory and not the Memberprovider.

    The problem is somewhere over here:

    using eFocus.Peevee.Models;
    using System;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.Security;
    using Umbraco.Web;
    
    namespace Umbraco.Extensions.Utilities
    {
        public class Global : UmbracoApplication
        {
            /// <summary>
            ///     With this function we will extend the current principal by our own custom model.
            ///     This makes it possible so you can use @User.Firstname in Razor as example. See the
            ///     available variables in Models/MemberModel.cs
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
            {
                HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    
                // First we will check someone is FormsAuthenticationed.
                if (authCookie != null)
                {
                    // Then we will decrypt the cookie because it was encrypted at login.
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    
                    // Now we will parse the userData that was serialized back to an object.
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    MemberModel serializeModel = serializer.Deserialize<MemberModel>(authTicket.UserData);
    
                    // We will fill our own custom principal with the information from the cookie.
                    CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
                    newUser.Name = serializeModel.Name;
                    newUser.Firstname = serializeModel.Firstname;
                    newUser.Lastname = serializeModel.Lastname;
                    newUser.Mail = serializeModel.Mail;
                    newUser.Company = serializeModel.Company;
    
                    // Here we bind the custom principal to the HttpContent.
                    HttpContext.Current.User = newUser;
                }
            }
        }
    }
    

    Can you help me out? Still haven't fixed this :(

  • Jan A 63 posts 268 karma points
    Sep 19, 2016 @ 11:42
    Jan A
    0

    Can't say I've worked with extending Umbraco membership provider.

    Found this: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/76777-member-ad-authentication-not-logging-in-member#comment-245396

    Now for Members, visitors to the site, there IS a bit of a proof of concept for using ASP.Net Identity for members in Umbraco, core member Shannon has a repo on GitHub here:

    https://github.com/Shazwazza/UmbracoIdentity

    Note that if you're running umbraco before version 7.4.2 you need to use older version of the package. Maybe you can find some answers there?

  • Yanick Van Barneveld 27 posts 148 karma points
    Sep 21, 2016 @ 13:38
    Yanick Van Barneveld
    0

    I have fixed it by not overwriting the HttpContext.Current.User but saved it to a item using: HttpContext.Current.Items["IUser"] = newUser;. A little bit dirtier but it works! :)

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 23, 2019 @ 17:43
    Simon Dingley
    0

    In v8 is there any way or a better way to achieve what the OP was trying to do?

    I was doing the following:

    IMyCustomPrincipal newUser = new MyCustomPrincipal(authTicket.Name);
    if(userData != null)
    { 
        newUser.Id = userData.Id;
        newUser.Name = userData.Name;
        newUser.TenantReference = userData.TenantReference;
    }
    
    // Set the current user to our custom IPrinciple
    HttpContext.Current.User = newUser;
    

    But setting the HttpContext.Current.User obviously destroys any back-office session you might have so you can either be logged in to the front end or the back end but not both.

Please Sign in or register to post replies

Write your reply to:

Draft