Код: Выделить всё
using (var scope = temporaryProvider.CreateScope())
{
var dataverseUtilityService = scope.ServiceProvider.GetRequiredService();
// Fetch tenantId and clientId from DataverseUtilityService
var configuration = dataverseUtilityService.GetConfiguration();
var tenantId = configuration.msys_AzureTenantId;
var clientId = configuration.msys_AzureClientId;
// Swagger configuration with OAuth2 using Authorization Code Flow and client secret
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
// Define OAuth2 Authorization Code Flow (without PKCE)
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Description = "OAuth 2.0 Authorization Code Flow (with client secret)",
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize"),
TokenUrl = new Uri($"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token"),
Scopes = new Dictionary(StringComparer.Ordinal)
{
{ $"api://{clientId}/access_the_api/.default", "Access API" },
},
},
},
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2",
},
Scheme = "oauth2",
Name = "oauth2",
In = ParameterLocation.Header,
},
new List { $"api://{clientId}/access_the_api/.default" }
},
});
});
}
// Build the app after adding Swagger configuration
var app = builder.Build();
// Enable Swagger UI and Swagger endpoint in all environments
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MedlemssystemApi V1");
c.OAuth2RedirectUrl(url: "https://app-dev-medlemssystemapi.azurewebsites.net/swagger/oauth2-redirect.html");
});
// Basic middleware for all environments
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// Map controllers and APIs
app.MapControllers();
// Run the app
app.Run();
[img]https:// i.sstatic.net/9aaaFAKN.png[/img]

и вручную настройте следующую область действия:
[img]https://i. sstatic.net/Jp8qxyP2.png[/img]
Проблема возникает, когда я пытаюсь пройти аутентификацию в пользовательском интерфейсе Swagger:

Происходит следующая ошибка:
Код: Выделить всё
auth errorError: response status is 400, error: invalid_request, description: AADSTS9002326: Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. Request origin: 'https://app-dev-medlemssystemapi.azurewebsites.net'. Trace ID: eeb5797b-7c22-4162-a2f9-ae086eef1300 Correlation ID: 5a6cbca2-73c5-4ad8-8352-58d80ad6d47f Timestamp: 2024-09-19 14:41:59Z
Подробнее здесь: https://stackoverflow.com/questions/790 ... swagger-ui