作者:fion依依315 | 来源:互联网 | 2023-08-13 15:55
在使用asp.netcore的session存取验证码时,发现后面获取不到,参考别的把services.Configure<CookiePolicyO
在使用asp.net core 的session存取验证码时,发现后面获取不到,参考别的把services.Configure注释掉就可以了,cnm microsoft。microsoft经常变,无语!!!
https://blog.csdn.net/yenange/article/details/80954802
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using LJCMS.Models;
using UEditor.Core;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace LJCMS
{
public class Startup
{
public Startup(IConfiguration configuration)
{
COnfiguration= configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.Configure(optiOns=>
//{
// // This lambda determines whether user consent for non-essential COOKIEs is needed for a given request.
// options.CheckCOnsentNeeded= cOntext=> false;
// options.MinimumSameSitePolicy = SameSiteMode.None;
//});
// 使用ueditor
services.AddUEditorService();
//使用asp.net core 会话功能
services.AddDistributedMemoryCache();
services.AddSession(optiOns=>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(100);
options.COOKIE.HttpOnly= true;
// Make the session COOKIE essential
options.COOKIE.IsEssential = true;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext(optiOns=>
options.UseSqlite(Configuration.GetConnectionString("LJCMSContext2")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "upload")),
RequestPath = "/upload",
OnPrepareResponse= ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=36000");
}
});
app.UseStaticFiles();
app.UseSession();
//app.UseCOOKIEPolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{cOntroller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{cOntroller=Home}/{action=Index}/{id?}"
);
});
}
}
}