我在ASP.NET Core 3.1 Web Api项目中的承载身份验证有问题。
我这样配置了身份验证:
with open("Test.txt", "r") as f1: lines = f1.readlines()
count_lines = len(content)
with open("Test.txt", "r") as f2: letters = f2.read()
count_letters = int((len(letters) - count_lines) / count_lines)
i = 0
j = 1
list = []
for x in range(1, count_letters):
AA = 0
AT = 0
TA = 0
TT = 0
for line in content:
if line[i] == "A" and line[j] == "A": AA = AA + 1
if line[i] == "A" and line[j] == "T": AT = AT + 1
if line[i] == "T" and line[j] == "A": TA = TA + 1
if line[i] == "T" and line[j] == "T": TT = TT + 1
sum = count_lines
p_AA = 100*AA/sum
p_AT = 100*AT/sum
p_TA = 100*TA/sum
p_TT = 100*TT/sum
i = i + 1
j = j + 1
此外,我在控制器中添加了services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = AuthHelper.Issuer,
ValidateAudience = true,
ValidAudience = AuthHelper.Audience,
ValidateLifetime = true,
IssuerSigningKey = AuthHelper.GetSymmetricSecurityKey(),
ValidateIssuerSigningKey = true,
};
});
属性,在Configure方法中添加了[Authorize]
。
我在Postman中配置了请求。我尝试在“授权”标签和手动方式(添加“授权”标头)中配置授权。结果相同:我得到正确答案,没有任何错误。
但是,当我从Flutter应用发送请求时,出现“ 404 Not Found”错误。
从flutter应用程序获取请求:
app.UseAuthorization();
有什么想法吗?我想念什么?
答案 0 :(得分:0)
我知道问题出在哪里。 我的控制器仍然使用Cookies身份验证方法(而非承载)。
解决方案:将[Authorize]
替换为[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
为什么邮递员的请求正常工作?
邮递员设置cookie并记住cookie用于其他请求。
因此,当我测试登录控制器时,Postman将.AspNetCore.Identity.Application
cookie添加到了自己的收藏集中。