Copied to clipboard

Flag this post as spam?

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


  • Filipe Sousa 43 posts 205 karma points
    Jan 28, 2020 @ 18:13
    Filipe Sousa
    0

    Get current user

    Hi there,

    how do I get the current user within a DelegatingHandler when loading a page in the backoffice?

    I'm injecting the IUmbracoContextFactory and fetching the user like so, but it's always null.

    using (var contextReference = _umbracoContextFactory.EnsureUmbracoContext())
            {
                var user = contextReference.UmbracoContext.Security.CurrentUser;
            }
    

    Much appreciated.

  • Ruben Meintema 9 posts 100 karma points
    Apr 22, 2020 @ 12:56
    Ruben Meintema
    0

    Hi Filipe,

    On the frontend of the website the user would be null, in the Umbraco backoffice you would be able to get a proper user.

    Cheers, Ruben

  • Pagggy 28 posts 71 karma points
    Apr 22, 2020 @ 13:14
    Pagggy
    0

    Hi @Filipe Sousa, how did you managed to add DelegatingHandler in umbraco 8. I am struggling to migrate my code from v7 to v8 for custom delegatinghandler.

  • Ruben Meintema 9 posts 100 karma points
    Apr 23, 2020 @ 07:31
    Ruben Meintema
    0

    Hi @Filipe Sousa,

    There is also a difference between trying to a User (an Umbraco CMS user) and a Member (like a user that is logged in to the frontend of the website). The former will not be resolved with "contextReference.UmbracoContext.Security.CurrentUser", while the latter can be resolved like this.

    Cheers, Ruben

  • Enkosi 11 posts 86 karma points
    Dec 17, 2020 @ 07:11
    Enkosi
    2

    Filipe, you can try the following to get the current user:

    var wrapper = new HttpContextWrapper(HttpContext.Current);
    var currentUserObj = wrapper.GetCurrentIdentity(true);
    

    Hope it helps.

  • Shivani Rajput 27 posts 97 karma points
    Mar 28, 2023 @ 05:55
    Shivani Rajput
    0

    Hello, Enkosi

    it's not working in Umbraco v11. any other solution are there?

  • Tor Langlo 191 posts 554 karma points
    Mar 28, 2023 @ 15:48
    Tor Langlo
    1

    Hi Shivani, here's one way of doing it:

    public class ContentSavingHandler : INotificationHandler<ContentSavingNotification>
    {
        public ContentSavingHandler(IHttpContextAccessor httpContextAccessor)
        {
            HttpContextAccessor = httpContextAccessor;
        }
    
        private readonly IHttpContextAccessor HttpContextAccessor;
    
        public void Handle(ContentSavingNotification notification)
        {
            var services = HttpContextAccessor.HttpContext.RequestServices;
            var memberManager = services.GetRequiredService<IMemberManager>();
            MemberIdentityUser currentMember = memberManager.GetCurrentMemberAsync()?.Result;
        }
    }
    
  • Shivani Rajput 27 posts 97 karma points
    Mar 29, 2023 @ 08:07
    Shivani Rajput
    0

    Thanks! Mr.Tor for your answer I have another question related to data-Source files. -> How we create data-Source file which is located form section. I created one data-Source file in which they ask for the connection string and table name. after saving that file we have one option to create a form from that file but after creating that I render macro at my content page but then it gives me the SQL error.

    So, my main question is how can I create data-Source files and how to use that files in my content page. I need a reference.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies