1

Somehow the same project we are working on with my colleague doesn't call the HTTP POST method that retrieves the token on my PC, but works fine on my colleague's PC.

The code is the same, but my output show me this pop-up:

enter image description here

The request works fine in Postman:

enter image description here

Have tested different browsers. We have deployed the package on the server and the token is retrieved correctly. Deploying the package locally shows the same error. Does something on my PC interfere with an HTTP request ?

Here is the code:

services
            .AddSwaggerGen(
                (c) =>
                {
                    var version = $"v{Assembly.GetEntryAssembly()?.GetVersionInfo()}";
                    c.OperationFilter<ChorusModeHeaderFilter>();
                    c.OperationFilter<AuthenticationFilter>();
                    c.OperationFilter<ErrorFilter>();
                    c.OperationFilter<MaintenanceFilter>();
                    c.SwaggerDoc(version, new OpenApiInfo { Title = appName, Version = version });
                    c.AddSecurityDefinition(
                        "Bearer",
                        new OpenApiSecurityScheme
                        {
                            Type = SecuritySchemeType.OpenIdConnect,
                            OpenIdConnectUrl = new Uri($"../../.well-known/openid-configuration", UriKind.Relative)
                        });
                    c.AddSecurityRequirement(
                        new OpenApiSecurityRequirement
                        {
                            {
                                new OpenApiSecurityScheme
                                {
                                    Reference = new OpenApiReference
                                    {
                                        Type=ReferenceType.SecurityScheme,
                                        Id="Bearer"
                                    }
                                },
                                Array.Empty<string>()
                            }
                        });
                    xmlDocumentationFiles
                        .Select((path) => Path.Combine(AppContext.BaseDirectory, path))
                        .ToList()
                        .ForEach((path) => c.IncludeXmlComments(path));
                });

and also:

public class AuthenticationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (!context.MethodInfo.GetCustomAttributes(true).OfType<AllowAnonymousAttribute>().Any())
        {
            operation.Responses.Add(
                $"{(int)HttpStatusCode.Unauthorized}",
                new OpenApiResponse { Description = "Unauthorized" });
        }
        else
        {
            // No authentication skip it
        }
    }
}
Helen
  • 87,344
  • 17
  • 243
  • 314
Rexhi
  • 191
  • 1
  • 9

1 Answers1

1

can you post the code from your startup.cs class in the API? (The request pipeline?) it looks like you have added support for BasicAuthentication?

You should use AddJwtBearer if your API should accept tokens.

Tore Nestenius
  • 16,431
  • 5
  • 30
  • 40