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.
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
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 IdentityIAuthenticationService
and if it fails we return aChallangeResult
. But I'm not sure how we could now write a handler and redirect a user to an Umbraco URL in their own language.After scrolling the internet I found some samples from dotnet.
you need to register this to the
IServiceCollection
is working on a reply...