如何与Angular 12应用程序共享ASP.NET Cookie

发布于 2025-02-10 20:14:31 字数 1086 浏览 0 评论 0原文

如何与Angular 12应用程序共享ASP.NET Cookie?

我在ASP.NET应用程序中有以下代码来创建Cookie:

 HttpCookie cookie = new HttpCookie(".AspNet.SharedCookie", DateTime.Now.ToString())
            {
                Expires = DateTime.Now.AddHours(24),
                Secure = true,
                Domain = "localhost",
                Path = "/",
            };
            HttpContext.Response.Cookies.Add(cookie);
            return Content("Welcome, new visitor!");

我正在尝试在Angular 12应用程序中阅读此Cookie。

这是我的代码:

import { CookieService } from '../../../../node_modules/ngx-cookie-service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private cookieService: CookieService) { }
  cookieValue: string = ""; 
  ngOnInit(): void {
    debugger;
    this.cookieValue = this.cookieService.get('.AspNet.SharedCookie');
    console.log(this.cookieService.get('.AspNet.SharedCookie'));
  }
}

我总是把Cookievalue作为空?

How to share Asp.net cookie with Angular 12 application?

I have the following code in my ASP.NET application to create cookie:

 HttpCookie cookie = new HttpCookie(".AspNet.SharedCookie", DateTime.Now.ToString())
            {
                Expires = DateTime.Now.AddHours(24),
                Secure = true,
                Domain = "localhost",
                Path = "/",
            };
            HttpContext.Response.Cookies.Add(cookie);
            return Content("Welcome, new visitor!");

I am trying to read this cookie in Angular 12 application.

This is my code:

import { CookieService } from '../../../../node_modules/ngx-cookie-service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private cookieService: CookieService) { }
  cookieValue: string = ""; 
  ngOnInit(): void {
    debugger;
    this.cookieValue = this.cookieService.get('.AspNet.SharedCookie');
    console.log(this.cookieService.get('.AspNet.SharedCookie'));
  }
}

I am always getting cookieValue as empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文