Copied to clipboard

Flag this post as spam?

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


  • Ken Schnell 35 posts 147 karma points
    Aug 04, 2020 @ 18:21
    Ken Schnell
    1

    Hooking Application_Startup in Umbraco Version 8.6.3

    I am using Umbraco Version 8.6.3 and looking at trying to get my Captcha code running , one of the things the library I am using for ReCaptcha , it has a static config class instantiated with in the Global.asax.cs file App_Startup - the problem is I do not see that code running at all - and it appears that this is the case because my static config is basically null , and when I place

    System.Diagnostics.Debugger.Break();
    

    The code does not stop either and it should ..if it is being called but it does not.

    In this Web Page Link I found something that seems to be the right way to gain access at startup - it points to a link that no longer exists - so I have no idea about the sample there but searched for that and I found another link based on the info provided
    Version 7 Application Startup

    Which led me to the Version 8 how to hook into Umbraco Start Events Unfortunately the example there and at the BLOG listings provided do not show a good example of hooking into the application_startup .

    So I did find an event

    UmbracoApplication.ApplicationInit += UmbracoApplication_ApplicationInit; 
    

    The problem with this , is that the event fires multiple times through out the life cycle at least twice on startup (which is odd since it is an Init Method .. it also appears to fire at other times.

    I am hoping that this is the right procedure to accomplish the task. I have tried this out and it seems to work in my development code - if this is not the proper method can someone please assist or state this to be correct process (if not the best implementation I am open..)

        public class ApplicationComposer : ComponentComposer<ApplicationComponent>, IUserComposer
        {
            public override void Compose(Composition composition)
            {
                // ApplicationStarting event in V7: add IContentFinders, register custom services and more here
               // I was reading here 
                // https://our.umbraco.com/forum/developers/extending-umbraco/10564-Accessing-globalasax-events-
                base.Compose(composition);
            }
        }
    
    
        public class ApplicationComponent : IComponent
        {
            public void Initialize()
            {
                // ApplicationStarted event in V7: add your events here
                WebStartInstance webStart = new WebStartInstance();
    
    // This event fires multiple times - at least 2 times on load , and I thought on page refresh but i need to check that out.
                UmbracoApplication.ApplicationInit += UmbracoApplication_ApplicationInit;
    
                webStart.AppStartOverride();
    
            }
    
            private void UmbracoApplication_ApplicationInit(object sender, EventArgs e)
            {
                var mySecretTest = "Test" ; 
    
                var tmp = mySecretTest;
    
            }
    
            public void Terminate()
            { }
        }
    

    https://our.umbraco.com/documentation/reference/events/application-startup

  • James Carter 3 posts 115 karma points
    Aug 05, 2020 @ 15:34
    James Carter
    101

    Hi Ken,

    It looks to me as if you'd be fine executing your startup code directly within the Initialize method of your Component (there's no need to hook into any other events as you only need your code to execute once).

    Here's the documentation for composing and components: https://our.umbraco.com/documentation/implementation/composing/

    Typically they are used to hook up custom code to Umbraco events but there's nothing wrong in using one to configure your ReCaptcha on Initialize. Note that the Initialize method is executed once when Umbraco starts up.

    Alternatively, you could access your static class from within your Composerand configure it there without the need for a Component (maybe someone will weigh in on their preference between the two for this use case).

    As a side note I'd avoid using statics where possible. What package are you using? There may be a way to set it up using Dependency Injection. More on DI: https://our.umbraco.com/documentation/reference/using-ioc/

    I hope that helps - let me know how it goes! :)

    Edit: Your ApplicationComposer class does not need to implement an IUserComposer if it inherits a ComponentComposer<T> too as that in itself is an implementation of IUserComposer that automatically registers your component for you :)

    It can refactored to be public class ApplicationComposer : ComponentComposer<ApplicationComponent> {}

  • Ken Schnell 35 posts 147 karma points
    Aug 05, 2020 @ 18:07
    Ken Schnell
    0

    Thank you for your knowledge and assistance - it is a big help.

    I am using https://www.nuget.org/packages/reCaptcha.Google.Mvc/ https://github.com/xsoheilalizadeh/reCaptcha.Google.Mvc

    It was designed for previous .NET versions - that utilized the global.asax for startup - but what I really like is that I don't have to do a lot of explicit implementation of the reCaptcha and only need to decorate the called method of my controller with the captcha. I am also posting here in case someone else is trying to do the same thing and wants to see how I did it..

    My Razor cshtml

    @using (Html.BeginUmbracoForm<MyWeb.Controllers.ContactSurfaceController>(
                            "controllerAction"))
                        {
    
    @HTML.RenderCaptchaScript()
    @Html.RenderCaptcha("google_captcha_validation", "sitekey")
    
                                    <button class="btn btn-primary"
                                            id="btnSubmit"
                                            type="submit"
                                            value="submit"
                                            data-sitekey="sitekey"
                                            data-action='controllerAction'>
                                        Submit
                                    </button>
    }
    

    Umbraco.Web.Mvc.SurfaceController

        public class mySurfaceController : Umbraco.Web.Mvc.SurfaceController
        {
    
            [HttpPost]
            [GoogleCaptcha]
            public ActionResult controllerAction(MyFormViewModel formData)
            {
    
            }
       }
    

    I am setting the configuration parameters in the Initialize method of the Composer, just as you had stated - because I only wanted it 1 time.

        public void RegisterCaptcha()
        {
            var captchaConfig = new Google.ReCaptcha.Mvc.Configuration.GoogleCaptchaConfig(Google.ReCaptcha.Mvc.Configuration.CaptchaTheme.Dark, 2);
    
            captchaConfig.EnableInDebuggingMode = true;
            captchaConfig.Secretkey = "V2SecretKey";
            captchaConfig.Sitekey = "V2SiteKey";
            captchaConfig.OnLoadCallback = "onload";
            captchaConfig.Theme = Google.ReCaptcha.Mvc.Configuration.CaptchaTheme.Dark;
            captchaConfig.Render = "onload";
            captchaConfig.Language = "en";
            captchaConfig.ValidationInputName = "google_captcha_validation";
            captchaConfig.ValidationMessage = "Please confirm your Identity.";
    
    
            Google.ReCaptcha.Mvc.Configuration.GoogleCaptchaConfiguration.Register(captchaConfig);
    
    
        }
    

    I will look into the Dependency Injection Link and since I don't know alot about DI will look at the benefits and reasons for it ..as well. Thank you ..

  • Nurhak Kaya 53 posts 147 karma points MVP 3x c-trib
    Nov 07, 2023 @ 16:38
    Nurhak Kaya
    0

    Thank you, James, your answer has helped me to fix my issue. Also thank you Ken for asking this question.

Please Sign in or register to post replies

Write your reply to:

Draft