我已經(jīng)為此奮斗了幾個小時,似乎無法找出為什么我對[Authorize]啟用的端點的所有調(diào)用都失敗并出現(xiàn) 401。在我的 .Net Core 2.2 Web API 項目的 Startup.cs 中,我設(shè)置了身份驗證:public void ConfigureServices(IServiceCollection services) { var jwtSettings = new JwtSettings(); Configuration.Bind(nameof(jwtSettings), jwtSettings); services.AddSingleton(jwtSettings); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Add the JWT Bearer token configuration services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("craigcraigcraigcraigcraigcraig")),//jwtSettings.Secret)), ValidateIssuer = false, ValidateAudience = false, RequireExpirationTime = false, ValidateLifetime = true }; }); services.AddSwaggerGen(x => { x.SwaggerDoc("v1", new Info { Title = "My Backend", Version = "v1" }); var security = new Dictionary<string, IEnumerable<string>> { {"Bearer", new string[0]} }; }); }請注意,我對我的密鑰進行了硬編碼,因為我不確定這是否是問題所在。該令牌返回到我的 Swagger 前端,看起來不錯。
1 回答

江戶川亂折騰
TA貢獻1851條經(jīng)驗 獲得超5個贊
你已經(jīng)在那里了,但是curl
有一個拼寫錯誤,更改Authorisation
為Authorization
:
curl -X GET“ https://localhost:44370/api/accounts ”-H“接受:application/json”-H“授權(quán):Bearer eyJ.....
- 1 回答
- 0 關(guān)注
- 143 瀏覽
添加回答
舉報
0/150
提交
取消