0

我有创建 cookie 的代码,如您所见并为其附加一个值:

Response.Cookies.Append("rudeCookie", "I don't need no user to tell me it's ok.", options);
var AccessToken = Request.Cookies["rudeCookie"];

附加后,值为Request.Cookies["rudeCookie"]null - 为什么?

这是我的startup文件:

public void ConfigureServices(IServiceCollection services)
{ 
    services.AddMvc();
    services.AddControllersWithViews();
           
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddScoped<saamieServices.IComplicationClient, saamieServices.ComplicationClient>();
    services.AddScoped<saamieServices.IIdentityClient, saamieServices.IdentityClient>();

    // services.Configure<CookiePolicyOptions>(options =>
    // {
    //    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    //    options.CheckConsentNeeded = context => true;
    //    options.MinimumSameSitePolicy = SameSiteMode.None;
    // });

    services.ConfigureApplicationCookie(option =>
            {
                option.Cookie.Name = "User";
                option.LoginPath = "User/UserRegister";
            });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
}
4

1 回答 1

0

据我所知,cookie 基于浏览器并在客户端浏览器加载后被初始化。

于 2021-04-11T07:28:35.513 回答