C# ASP.NET核心Web应用程序找不到cookie值。我错过了什么?

C# ASP.NET核心Web应用程序找不到cookie值。我错过了什么?,c#,asp.net,asp.net-core,cookies,session-cookies,C#,Asp.net,Asp.net Core,Cookies,Session Cookies,我已经从ASP.NET web表单应用程序创建了一个简单的web cookie。我正在尝试从单独的.NET核心web应用程序检索此cookie。每当我尝试这样做时,.NET核心应用程序总是为cookie返回空值 以下是在ASP.NET web表单应用程序中创建cookie的方式: protected void btn1_Click(object sender, EventArgs e) { HttpCookie Abc = new HttpCookie(

我已经从ASP.NET web表单应用程序创建了一个简单的web cookie。我正在尝试从单独的.NET核心web应用程序检索此cookie。每当我尝试这样做时,.NET核心应用程序总是为cookie返回空值

以下是在ASP.NET web表单应用程序中创建cookie的方式:

 protected void btn1_Click(object sender, EventArgs e)
        {
            HttpCookie Abc = new HttpCookie("Abc");
            DateTime now = DateTime.Now;

            //Abc Set the cookie value.
            Abc.Value = txt1.Text;
            // Set the cookie expiration date.
            Abc.Expires = now.AddMinutes(1);

            // Add the cookie.
            Response.Cookies.Add(Abc);

       }
public void OnGet()
        {

         if (HttpContext.Request.Cookies["Abc"] != null)
            {
                Message = "ya";
            }
            else
            {
                Message = "no";
            }
        }
以下是我试图从.NET核心应用程序中读取此“Abc”cookie的方式:

 protected void btn1_Click(object sender, EventArgs e)
        {
            HttpCookie Abc = new HttpCookie("Abc");
            DateTime now = DateTime.Now;

            //Abc Set the cookie value.
            Abc.Value = txt1.Text;
            // Set the cookie expiration date.
            Abc.Expires = now.AddMinutes(1);

            // Add the cookie.
            Response.Cookies.Add(Abc);

       }
public void OnGet()
        {

         if (HttpContext.Request.Cookies["Abc"] != null)
            {
                Message = "ya";
            }
            else
            {
                Message = "no";
            }
        }
以下是ASP.NET核心应用程序的Startup.cs详细信息:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddDistributedMemoryCache();
          //  services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddHttpContextAccessor();
            services.AddSession(options =>
            {
                options.Cookie.HttpOnly = true;
                // Make the session cookie essential
                options.Cookie.IsEssential = true;
            });

            services.Configure<CookiePolicyOptions>(options =>
            {
                // No consent check needed here
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSession();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
public void配置服务(IServiceCollection服务)
{
services.AddRazorPages();
AddDistributedMemoryCache();
//services.TryAddSingleton();
AddHttpContextAccessor();
services.AddSession(选项=>
{
options.Cookie.HttpOnly=true;
//使会话cookie必不可少
options.Cookie.IsEssential=true;
});
配置(选项=>
{
//这里不需要同意检查
options.checkApprovered=context=>false;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
});
}
运行ASP.NET Core应用程序时,我能够按预期在浏览器中找到cookie:

我花了好几个小时研究这个问题,但没有成功。关于为什么我不能从.Net核心应用程序中读取cookie,有什么想法吗?我非常感谢您的反馈


谢谢大家!

如果您的web应用托管在同一域的子域中(例如app1.example.com和app2.example.com),则您可以通过将HttpCookie对象的“domain”属性设置为.example.com来轻松读取子域之间的Cookie

HttpCookie Abc = new HttpCookie("Abc");
DateTime now = DateTime.Now;
Abc.Domain = ".example.com";
//Abc Set the cookie value.
Abc.Value = txt1.Text;
// Set the cookie expiration date.
Abc.Expires = now.AddMinutes(1);

// Add the cookie.
Response.Cookies.Add(Abc);

您使用的是什么版本的.Net Core?嗨@EdneyHolder,我使用的是netcoreapp3.0Hi@Anuraj,我通过visual studio运行这两个应用程序-因此它们都在本地主机上运行。它应该是相同的-如果您使用不同的端口-可能是不同的域AHHH。谢谢你,阿努拉吉!