Using OpenId token validation in custom services. (Checking if user is authenticated) #13159
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Handled it with the DefaultHttpRequestInterceptor from hotchocolate. |
Beta Was this translation helpful? Give feedback.
Handled it with the DefaultHttpRequestInterceptor from hotchocolate.
public class AuthenticationInterceptor : DefaultHttpRequestInterceptor
{
public override ValueTask OnCreateAsync(HttpContext context, IRequestExecutor requestExecutor,
IQueryRequestBuilder requestBuilder,
CancellationToken cancellationToken)
{
var authenticationService = context.RequestServices.GetService();
var task = authenticationService.AuthenticateAsync(context, "Api");
task.Wait(cancellationToken);
var result = task.Result;
if (!result.Succeeded)
{
throw new QueryException(
ErrorBuilder.New()
.SetMessage("Resource is restricted to authenticated users")
.SetCode("UNAUTHENTICATED")
.Build());
}
return base.OnCreateAs…