Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationFailed Message #43

Open
renerlemes opened this issue Jun 9, 2024 · 0 comments
Open

ValidationFailed Message #43

renerlemes opened this issue Jun 9, 2024 · 0 comments

Comments

@renerlemes
Copy link

@mihirdilip when I use the context.ValidationFailed("Message") inside the OnValidateKey event, how to get the message in this custom middleware?

It´s not possible to get with PolicyAuthorizationResult.FailureReasons

public class AuthorizationMiddleware : IAuthorizationMiddlewareResultHandler
{
    private readonly IAuthorizationMiddlewareResultHandler _handler;

    public AuthorizationMiddleware()
    {
        _handler = new AuthorizationMiddlewareResultHandler();
    }

    public async Task HandleAsync(RequestDelegate requestDelegate, HttpContext httpContext, AuthorizationPolicy authorizationPolicy, PolicyAuthorizationResult policyAuthorizationResult)
    {
        if (!policyAuthorizationResult.Succeeded)
        {
            Models.ResponseDefault responseDefault = new(false, new[] { "O usuário não está autenticado" });

            httpContext.Response.ContentType = "application/json";
            httpContext.Response.StatusCode = 401;
            await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(responseDefault));

            return;
        }

        await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, policyAuthorizationResult);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant