0

I have created a sample duende identity server and I want to authorize my swagger apis from that I have used this in my program.cs

When I try to authorize it allows me to enter username and password and then it's redirect to swagger UI with saying "Auth error 400 invalid clien id"

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.Authority = "https://localhost:5443";
    options.Audience = "swagger.api";
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = "https://localhost:5443",
        ValidAudience = "swagger.api",
        //IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your signing key here")),
    };
})
.AddOpenIdConnect(options =>
{
    options.Authority = "https://localhost:5443";
    options.ClientId = "swagger";
    options.ClientSecret = "secret";
    options.ResponseType = "code";
    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.Scope.Add("openid");
    options.Scope.Add("profile");
    options.Scope.Add("swagger.api");
    
});
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "Protected API", Version = "v1" });
    options.AddSecurityDefinition(
        "oauth2",
        new OpenApiSecurityScheme
        {
            Type = SecuritySchemeType.OAuth2,
            Flows = new OpenApiOAuthFlows
            {
                AuthorizationCode = new OpenApiOAuthFlow
                {
                    AuthorizationUrl = new Uri(
                        $"{builder.Configuration.GetValue<string>("Identity:AuthorityUrl")}/connect/authorize"
                    ),
                    TokenUrl = new Uri(
                        $"{builder.Configuration.GetValue<string>("Identity:AuthorityUrl")}/connect/token"
                    ),
                    Scopes = new Dictionary<string, string>
                    {
                        { "swagger.api", "swagger.api" }
                    }
                }
            }
        }
    );
    options.OperationFilter<SwaggerOperationFilter>();
    options.SchemaFilter<SwaggerSchemaFilter>();
});
app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
        options.OAuthClientId("swagger");
        options.OAuthClientSecret("secret");
        options.OAuthAppName("Demo API - Swagger");
        options.OAuthUsePkce();

    });

Do I miss something here or do I do this in a wrong way as I'm getting invalid client issue always

0 Answers0