ASP.NETCore6基本身份验证问题Postman提交方法
创始人
2024-09-17 13:30:39
0

在ASP.NET Core 6中,开发人员可能遇到使用Postman进行基本身份验证的问题。当使用Postman提交一个包含基本身份验证标头的POST请求时,服务器会返回401未经授权的状态码。

解决此问题的方法是在Startup.cs文件中添加以下代码:

using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

namespace YourProjectNamespace { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
                ValidIssuer = Configuration["Jwt:Issuer"],
                ValidAudience = Configuration["Jwt:Issuer"],
                ValidateIssuer = true,
                ValidateAudience = true,
                IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
            };
        });
        
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthentication();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

}

这个代码对JWT身份验证作了如下配置:

  • 首先,我们在 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) 中添加JWT身份验证;

  • 然后,配置JWT身份验证选项。包括Issuer和Audience的检查,以及IssuerSigningKey的使用。

  • 最后,在Configure方法中使用app.UseAuthentication();添加身份验证中间件,以确保每次发送请求

相关内容

热门资讯

必备教程!pokernow辅助... 必备教程!pokernow辅助工具,aapoker透视脚本入口,竟然是真的有挂(有挂方式)1、操作简...
总算清楚!wepoker辅助分... 总算清楚!wepoker辅助分析器,wpk作弊最怕三个东西,好像真的是有挂(有挂技巧)1、点击下载安...
我来向大家传授!hhpoker... 我来向大家传授!hhpoker辅助靠谱吗,hhpoker有后台操控吗,切实是真的有挂(真的有挂)1、...
今日头条!fishpoker透... 今日头条!fishpoker透视,德扑圈透视,一直真的是有挂(有挂工具)1、打开软件启动之后找到中间...
玩家必看分享!hhpoker有... 玩家必看分享!hhpoker有透视功能吗,wepoker辅助真的假的,确实真的是有挂(存在有挂)1、...
推荐一款!HH平台挂,hhpo... 推荐一款!HH平台挂,hhpoker有后台操控吗,一贯真的有挂(有挂解密)1、下载好透视辅助下载之后...
玩家爆料!德扑圈透视挂,哈糖大... 玩家爆料!德扑圈透视挂,哈糖大菠萝免费辅助器,竟然是有挂(有挂规律)哈糖大菠萝免费辅助器透视方法中分...
带你了解!wepoker俱乐部... 带你了解!wepoker俱乐部辅助,hhpoker买挂,切实真的是有挂(有挂技巧)在进入软件靠谱后,...
终于懂了!wepoker有没有... 终于懂了!wepoker有没有机器人,hhpoker是真的假的,其实有挂(了解有挂)1、首先打开辅助...
分享一款!wepoker有什么... 分享一款!wepoker有什么规律,xpoker辅助器,一贯真的有挂(有挂总结)1、破解器简单,机器...