Postman 为什么净核心返回0到角?

Postman 为什么净核心返回0到角?,postman,asp.net-core-webapi,angular8,Postman,Asp.net Core Webapi,Angular8,我有一个应用程序与角度和网络核心网络api 3。 我的问题是,当令牌在angular中过期时,我会得到一个状态代码0,但当使用postman时,它实际上会返回代码401(未经授权)。 会发生什么?非常感谢你的帮助 Start.cs public void ConfigureServices(IServiceCollection services) { var appSettingsJson = AppSettingsJson.GetAppSettings();

我有一个应用程序与角度和网络核心网络api 3。 我的问题是,当令牌在angular中过期时,我会得到一个状态代码0,但当使用postman时,它实际上会返回代码401(未经授权)。 会发生什么?非常感谢你的帮助

Start.cs

public void ConfigureServices(IServiceCollection services)
    {
        var appSettingsJson = AppSettingsJson.GetAppSettings();
        var tokenKey = appSettingsJson["Token:Key"];
        services.AddMvc()
               .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
               .AddNewtonsoftJson(ConfigureJson).AddExtensions();
        services.AddSignalR();
        services.AddCors(options =>
        {
            options.AddPolicy("EnableCORS", builder => { builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod(); });
            options.AddPolicy("NoRestrictions", builder => { builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod(); });
        });

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddJwtBearer(options =>
         {
             options.TokenValidationParameters = new TokenValidationParameters
             {
                 ValidateIssuer = true,
                 ValidateAudience = true,
                 ValidateLifetime = true,
                 ValidateIssuerSigningKey = true,
                 ValidIssuer = "http://localhost:5000",
                 ValidAudience = "http://localhost:5000",
                 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenKey))
             };
         });
        services.AddControllers();
    }



    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {

        if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
        else app.UseHsts();
        app.Use(async (ctx, next) =>
        {
            await next();
            if (ctx.Response.StatusCode == 204)
            {
                ctx.Response.ContentLength = 0;
            }
        });
        app.UseRouting();
        app.UseAuthorization();
        app.UseCors("EnableCORS");
        app.UseAuthentication();
        app.UseHttpsRedirection();
        //app.UseMvc();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<NotifyHub>("notify").RequireCors("EnableCORS");
            endpoints.MapHub<NotifyActionHub>("notifyaction").RequireCors("EnableCORS");
        });
    }
public void配置服务(IServiceCollection服务)
{
var appSettingsJson=appSettingsJson.GetAppSettings();
var tokenKey=appsetingsjson[“Token:Key”];
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(ConfigureJson).AddExtensions();
services.AddSignalR();
services.AddCors(选项=>
{
options.AddPolicy(“EnableCORS”,builder=>{builder.WithOrigins(“*”).AllowAnyHeader().AllowAnyMethod();});
options.AddPolicy(“NoRestrictions”,builder=>{builder.WithOrigins(“*”).AllowAnyHeader().AllowAnyMethod();});
});
services.AddSingleton();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(选项=>
{
options.TokenValidationParameters=新的TokenValidationParameters
{
validateisuer=true,
ValidateAudience=true,
ValidateLifetime=true,
ValidateSuersigningKey=true,
ValidisUser=”http://localhost:5000",
有效性=”http://localhost:5000",
IssuerSigningKey=new-SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenKey))
};
});
services.AddControllers();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境、iLogger工厂)
{
if(env.IsDevelopment())app.UseDeveloperExceptionPage();
else app.UseHsts();
应用程序使用(异步(ctx,下一步)=>
{
等待下一个();
如果(ctx.Response.StatusCode==204)
{
ctx.Response.ContentLength=0;
}
});
app.UseRouting();
app.UseAuthorization();
应用程序UseCors(“EnableCORS”);
app.UseAuthentication();
app.UseHttpsRedirection();
//app.UseMvc();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapHub(“通知”).RequireCors(“启用”);
endpoints.MapHub(“notifyaction”).RequireCors(“EnableCORS”);
});
}

我可以解决它。我这样做是为了改变services.AddCors和app.UseCors(“EnableCORS”)指令的位置;到了前线,一切都很顺利

 public void ConfigureServices(IServiceCollection services)
    {
        var appSettingsJson = AppSettingsJson.GetAppSettings();
        var tokenKey = appSettingsJson["Token:Key"];
        *services.AddCors(options =>
        {
            options.AddPolicy("EnableCORS", builder => { builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); });
        });*
        services.AddMvc()
               .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
               .AddNewtonsoftJson(ConfigureJson).AddExtensions();
        services.AddSignalR();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddJwtBearer(options =>
         {
             options.TokenValidationParameters = new TokenValidationParameters
             {
                 ValidateIssuer = true,
                 ValidateAudience = true,
                 ValidateLifetime = true,
                 ValidateIssuerSigningKey = true,
                 ValidIssuer = "http://localhost:5000",
                 ValidAudience = "http://localhost:5000",
                 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenKey))
             };
         });
        services.AddControllers();
    }



    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {

        if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
        else app.UseHsts();
        app.UseCors("EnableCORS");

        app.Use(async (ctx, next) =>
        {
            await next();
            if (ctx.Response.StatusCode == 204)
            {
                ctx.Response.ContentLength = 0;
            }
        });
        app.UseRouting();
        app.UseAuthorization();
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers().RequireCors("EnableCORS");
            endpoints.MapHub<NotifyHub>("notify").RequireCors("EnableCORS");
            endpoints.MapHub<NotifyActionHub>("notifyaction").RequireCors("EnableCORS");
        });

    }
public void配置服务(IServiceCollection服务)
{
var appSettingsJson=appSettingsJson.GetAppSettings();
var tokenKey=appsetingsjson[“Token:Key”];
*services.AddCors(选项=>
{
options.AddPolicy(“EnableCORS”,builder=>{builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();});
});*
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(ConfigureJson).AddExtensions();
services.AddSignalR();
services.AddSingleton();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(选项=>
{
options.TokenValidationParameters=新的TokenValidationParameters
{
validateisuer=true,
ValidateAudience=true,
ValidateLifetime=true,
ValidateSuersigningKey=true,
ValidisUser=”http://localhost:5000",
有效性=”http://localhost:5000",
IssuerSigningKey=new-SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenKey))
};
});
services.AddControllers();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境、iLogger工厂)
{
if(env.IsDevelopment())app.UseDeveloperExceptionPage();
else app.UseHsts();
应用程序UseCors(“EnableCORS”);
应用程序使用(异步(ctx,下一步)=>
{
等待下一个();
如果(ctx.Response.StatusCode==204)
{
ctx.Response.ContentLength=0;
}
});
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseEndpoints(端点=>
{
endpoints.MapControllers().RequireCors(“EnableCORS”);
endpoints.MapHub(“通知”).RequireCors(“启用”);
endpoints.MapHub(“notifyaction”).RequireCors(“EnableCORS”);
});
}

看起来您的请求甚至没有到达后端,可能是CORS问题。在这里看到更多