0

将 .NET Core 2.0 与 Jwt 不记名身份验证一起使用。

当我在使用 IIS Express 的 localhost 上运行时,我的登录功能可以无缝运行。

但是,当我部署到 Prod Server 时,该站点允许我登录,但在我得到“401 Unauthorized”之后。

即使超时时间设置为登录后 30 分钟,并且在 Prod 服务器上设置正确。

好像我缺少 IIS 中的设置。看到此链接并进行了更改,但问题仍然存在。 类似问题

任何指针表示赞赏。

启动.cs>>

public class Startup
{

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, 
            reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
        _env = env;
    }

    public IConfigurationRoot Configuration { get; }
    public IHostingEnvironment _env { get; }
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata = false;
                options.IncludeErrorDetails = true;

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = Configuration.GetSection("AppSettings")["WebApiBaseUrl"],
                    ValidAudience = Configuration.GetSection("AppSettings")["WebBaseUrl"],
                    IssuerSigningKey = TokenAuthOption.Key,
                    ClockSkew = TimeSpan.Zero                     
                };


            });

        services.AddMvc().AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new ReadOnlyJsonContractResolver();
            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        });
        services.AddOptions();
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddSingleton<IHostingEnvironment>(_env);
        services.AddMvcCore().AddJsonFormatters().AddXmlSerializerFormatters().AddFormatterMappings().AddXmlDataContractSerializerFormatters();
  }


   public void Configure(IApplicationBuilder app, IHostingEnvironment env, DBContext context)
   {
              app.UseCors("CorsPolicy");
              app.UseSession();
              app.UseAuthentication();
              app.UseMvc();
              app.UseStaticFiles();


    }

}

谢谢迈尔

4

0 回答 0