gpt4 book ai didi

session - TempData Cookie问题。请求 header 的大小太长

转载 作者:行者123 更新时间:2023-12-01 03:02:58 28 4
gpt4 key购买 nike

Controller 中,我通过cookies将错误集合保存到TempData

var messages = new List<Message>();
...
TempData.Put("Errors", messages);

TempData.Put是一种扩展方法
public static class TempDataExtensions
{

public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class
{
tempData[key] = JsonConvert.SerializeObject(value);
}

public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class
{
tempData.TryGetValue(key, out object o);
return o == null ? null : JsonConvert.DeserializeObject<T>((string)o);
}
}

加载HTML后,我看到了

enter image description here

并创建了几个Cookie(Chrome开发者工具>应用程序>存储> Cookies)

enter image description here

我认为问题在于Cookie的总大小已达到某个Cookie大小限制。

所以我有两个问题:
是否可以更改cookie大小限制(例如,在web.config中)?
可以将 session 而不是cookie用于 TempData吗?

我尝试了第二种方法,如果我更改了 startup.cs 文件,
\\ ConfigureServices method

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();

services.AddSession();

\\ Configure method

app.UseSession();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

TempData仍在使用Cookies,我是否忘记了某些设置?

最佳答案

您可以使用HTTP cookie或 session 状态作为TempData的存储机制。基于cookie的TempData提供程序是默认设置。
您可以阅读有关Choose a TempData provider的更多信息。
根据docs中的以下示例,可以通过调用 TempData 扩展方法来启用基于 session 的AddSessionStateTempDataProvider提供程序。中间件的order很重要。

Be aware of DefaultTempDataSerializer limitations pointed out atbottom of this answer.


示例
Hers是 a working deployment,使用的是我为 设置的以下设置Srartup :
public class Startup
{
public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddSessionStateTempDataProvider();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else {
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes => {
routes.MapRoute(name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
HomeController :
public class HomeController : Controller
{
public IActionResult Index()
{
TempData["LargeData"] = new string('a', 1 * 1024 * 1024);
return View();
}
}
索引 View :
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome - @(((string)TempData["LargeData"]).Length)</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">
building Web apps with ASP.NET Core</a>.</p>
</div>
DefaultTempDataSerializer支持的类型
请注意 DefaultTempDataSerializer 支持的类型限制:
public override bool CanSerializeType(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

type = Nullable.GetUnderlyingType(type) ?? type;

return
type.IsEnum ||
type == typeof(int) ||
type == typeof(string) ||
type == typeof(bool) ||
type == typeof(DateTime) ||
type == typeof(Guid) ||
typeof(ICollection<int>).IsAssignableFrom(type) ||
typeof(ICollection<string>).IsAssignableFrom(type) ||
typeof(IDictionary<string, string>).IsAssignableFrom(type);
}

关于session - TempData Cookie问题。请求 header 的大小太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com