Copied to clipboard

Flag this post as spam?

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


  • Roy Berris 89 posts 578 karma points c-trib
    Feb 02, 2023 @ 12:15
    Roy Berris
    0

    Umbraco 10 - Redirect unauthenticated user (challange result) to an umbraco page

    Hello, does anyone have a good solution on redirecting a unauthenticated user to an umbraco page?

    We are using Umbraco Members but have custom roles, so we are not using the AuthorizeAttribute but a custom one instead. We do authenticate through the Identity IAuthenticationService and if it fails we return a ChallangeResult. But I'm not sure how we could now write a handler and redirect a user to an Umbraco URL in their own language.

  • Roy Berris 89 posts 578 karma points c-trib
    Feb 02, 2023 @ 14:37
    Roy Berris
    100

    After scrolling the internet I found some samples from dotnet.

    public class UnauthorizedHandler : IAuthorizationMiddlewareResultHandler
    {
        private readonly IAuthorizationMiddlewareResultHandler _handler;
    
        public UnauthorizedHandler()
        {
            // you can inject the UmbracoContext
            _handler = new AuthorizationMiddlewareResultHandler();
        }
    
        public async Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
        {
            // this means the user is authorized, and we shouldn't handle anything
            if (!authorizeResult.Forbidden || authorizeResult.AuthorizationFailure is null)
            {
                // Fallback to the default handler
                await _handler.HandleAsync(next, context, policy, authorizeResult);
                return;
            }
    
            // do anything with the umbraco context, get nodes, etc
            // add your own code here
            context.Request.Redirect("/your-umbraco-url");
        }
    }
    

    you need to register this to the IServiceCollection

    services.AddScoped<IAuthorizationMiddlewareResultHandler, UnauthorizedHandler>();
    
  • 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