Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中添加授权后的招摇过市问题_C#_Swagger_Swashbuckle.aspnetcore - Fatal编程技术网

C# 在.NET中添加授权后的招摇过市问题

C# 在.NET中添加授权后的招摇过市问题,c#,swagger,swashbuckle.aspnetcore,C#,Swagger,Swashbuckle.aspnetcore,首先,我想解释一下,这是一个我正在学习的Web API。这是我的第一个web API,我是新手。好吧,自从我使用承载令牌添加授权以来,API就可以很好地与Swagger配合使用 错误招摇抛出: 斯威格在错误中引用的类: using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using System; usi

首先,我想解释一下,这是一个我正在学习的Web API。这是我的第一个web API,我是新手。好吧,自从我使用承载令牌添加授权以来,API就可以很好地与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 });
        }
    }
}

正如我所说,我是个新手,如果有人能解释我为什么做错了,我会非常感激。谢谢

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

我投票结束这个问题,因为答案在错误消息中。@IanKemp请尊重,我说我是新手,我不知道如何解决它。