How to reject context with custom Status code(401) in OpenIdConnect server 2.0?

Viewed 18

I am using an older version of OpenIdConnect. This one. The problem is it always returns status 400 when there is an error or problem. But I need to send custom status like 401 shown below.

    if (user == null)
    {
        return LogInResponse.CreateWithAuthenticationError(AuthenticationErrors.Incorrect_User_Name_Or_Password, AuthenticationErrors.Incorrect_User_Name_Or_Password, 401);
   }
    

Also, when the context is rejected it cannot take any parameter that contains status.

    public async Task AuthenticateAsync(HandleTokenRequestContext context)
    {
        var grantType = context.Request.GrantType.ToLower().ToString();

        if (authenticationServices.ContainsKey(grantType))
        {
            var authenticationService = authenticationServices[grantType];

            var authenticationRequest = context.Request.GetProperty<AuthenticationRequest>(nameof(AuthenticationRequest));

            var logInResponse = await authenticationService.AuthenticateAsync(authenticationRequest);

            if (logInResponse.Success == false)
            {
                context.Reject(logInResponse.AuthenticationError.Key, logInResponse.AuthenticationError.Value);
                return;
            }

            context.Request.SetProperty("device_token", logInResponse.DeviceIdentifier);

            context.Validate(logInResponse.AuthenticationTicket);
        }
    }

Is there any way I can set and send the custom status code in the response?

0 Answers
Related