.NET 添加授权后的 Swagger 问题

Swagger Problem after Adding Authorization in .NET

首先,我想说明一下,这是我正在学习的WebAPI。这是我的第一个网站 API,而且我是新手。好吧,自从我使用不记名令牌添加授权的那一刻起,API 与 Swagger 一起工作得很好。

错误 Swagger 抛出:

class Swagger 在错误中指的是:

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IncidentManagerAPI.Security
{
    [Route("oauth")]
    public class OAuthController : Controller
    {
        [Route("token"), BasicAuthorization]
        public IActionResult Token()
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var authenticatedUser = new AuthenticatedUser(JwtBearerDefaults.AuthenticationScheme, true, "incidentmanagerapp");
            var accessToken = tokenHandler.WriteToken(tokenHandler.CreateToken(new SecurityTokenDescriptor
            {
                Subject = new System.Security.Claims.ClaimsIdentity(authenticatedUser),
                Expires = DateTime.UtcNow.AddMinutes(1),
                Issuer = "incidentmanagerapi",
                Audience = "incidentmanagerapp",
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("IncidentMaster2021.exe!")), SecurityAlgorithms.HmacSha256Signature),
                IssuedAt = DateTime.UtcNow
            }));
            return Ok(new { accessToken, token_type = "bearer", expires_in = 60 });
        }
    }
}

正如我所说,我很新,如果有人能解释我为什么做错了,我将非常感激。谢谢!

将 http 动词属性放在控制器方法 ex 上。 [HttpGet()]