Sobes.tech
Junior — Middle

What response code does the server return if no authentication token is provided in the request?

sobes.tech AI

Answer from AI

If an authentication token is not provided in an HTTP request, the server typically returns a response code 401 Unauthorized. This code indicates that authentication is required to access the resource, and the client has not provided or has provided incorrect credentials.

Example in C# using ASP.NET Core:

app.Use(async (context, next) => {
    if (!context.Request.Headers.ContainsKey("Authorization"))
    {
        context.Response.StatusCode = 401; // Unauthorized
        await context.Response.WriteAsync("Authentication token is missing.");
        return;
    }
    await next();
});

Thus, the absence of an authentication token results in a response with code 401.