我在跟踪:https://www.youtube.com/watch?v=r7gEKqSlb40并获得所有应用程序注册工作。我通过Swagger登录后,似乎Swagger没有将授权传递给控制器。
这是一段代码:
{
config.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Test", Version = "V1" });
config.AddSecurityDefinition("OAuth2", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
{
Description = "OAuth2 which uses authorization flow",
Name = "OAuth2",
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(builder.Configuration["SwaggerAzureAD:AuthorozationUrl"]),
TokenUrl = new Uri(builder.Configuration["SwaggerAzureAD:TokenUrl"]),
Scopes = new Dictionary<string, string>
{
{builder.Configuration["SwaggerAzureAD:Scope"], "Access API as user" }
}
}
}
});
config.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference{Type=ReferenceType.SecurityScheme, Id = "oauth2"}
},
new [] {builder.Configuration["SwaggerAzureAD:Scope"]}
}
});
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.OAuthClientId(builder.Configuration["SwaggerAzureAD:ClientId"]);
c.OAuthUsePkce();
c.OAuthScopeSeparator(" ");
});
}
我可以登录:
但我的端点似乎认为不需要:
知道我哪里做错了吗
这是我在控制器中的代码:生成的标准品:
namespace WebApplication2.Controllers
{
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
1条答案
按热度按时间2w2cym1i1#
和往常一样,这是个很蠢的错字
在这篇文章中:
ID是错误的,需要:
OAuth2
然后它被捡起来了。