我有一个简单的 Web api 项目,如下所示:
[Authorize]
[Route("Get")]
public ActionResult<string> SayHello()
{
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试用邮递员测试它。按照此处的步骤操作:https://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-testing-your-authorization-server-with-postman/
1) 发送以下请求并按预期接收令牌:
2) 尝试使用授权令牌发送另一个请求,如下所示:
为什么我会收到 401(未经授权)错误?WWW-Authenticate 响应标头显示:Bearer error="invalid_token", error_description="The Issuer is invalid"。我正在使用.Net Core 3.1。我已经把截图中的敏感信息注释掉了。
从 MVC 应用程序访问时,Web api 按预期工作。
这是启动代码:
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = identityUrl; //identityurl is a config item
options.RequireHttpsMetadata = false;
options.ApiName = apiName;
});
Run Code Online (Sandbox Code Playgroud) 我想在我的 Web 应用程序中实现 OAuth,为此我在我的应用程序中添加了以下代码startup.cs
public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CombiTime API v1.0", Version = "v1" });
c.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("http://localhost:4200/login"),
TokenUrl = new Uri("http://localhost:4200/connect/token")
}
}
});
c.OperationFilter<AuthorizeOperationFilter>();
c.AddSecurityRequirement(new OpenApiSecurityRequirement{
{
new OpenApiSecurityScheme{
Reference = new OpenApiReference{
Id = "Bearer", //The name of the previously defined security scheme.
Type = ReferenceType.SecurityScheme …Run Code Online (Sandbox Code Playgroud)