我想使用 javascript 设置一个 cookie,该 cookie 可用于浏览器中的所有选项卡。
我用过document.cookie = "tracker=ABC"
即使我也尝试设置路径,但结果仍然相同
document.cookie = "tracker=ABC;path=/"
但是,这似乎仅适用于设置它的选项卡。该 cookie 对浏览器中的其他选项卡不可见。
另外,我对cookie中的路径概念不是很清楚。如果有人能告诉我它作为辅助答案的用途,那就太好了
问候,
SAP学习者
所以我的问题是:如何设置一个适用于浏览器中所有选项卡的cookie
javascript cookies mozilla google-chrome internet-explorer-11
很简单,我想调用 PHPsession_start()而不将Set-Cookie响应标头返回到浏览器。看来调用 session_start() 隐式发送Set-Cookie标头。
有什么办法可以做到这一点吗?我尝试过在 session_start 中添加一个带有过期 cookie 的附加 Set-Cookie 标头以及其他方式,但没有任何效果,而且我找不到这方面的文档。
编辑/澄清:这样做的原因是我想(进行 AJAX 调用)返回数据(在同一域上),但返回时不设置会话 cookie。(接受的答案按需要工作)
大家好,我是 Nodejs 的初学者。我尝试过Express 中的cookie 解析器模块。他们正在使用 {signed:true} 设置签名 cookie。
首先澄清一下我。
1.为什么要使用签名cookie?和
2.签名cookie有什么用?
我已经检查了解决方案,但没有人说它有什么用?
我在程序中分别尝试了这两种情况,并且在不使用密钥和签名 cookie 的情况下没有收到任何错误:
任何帮助将不胜感激 提前致谢...
节点代码:
app.use(cookieparser("Youcansetanydataasasecret"));
const options={
maxAge:1000*60*5,
httpOnly:true,
signed:true
};
app.get('/setcookie',(req,res)=>{
res.cookie('cookieone','This is my first cookie',options);
res.send(`Cookie has been set...`);
});
app.get('/readcookie',(req,res)=>{
console.log(req.signedCookies['cookieone']);
res.send(req.signedCookies['cookieone']);
});Run Code Online (Sandbox Code Playgroud)
app.use(cookieparser());
const options={
maxAge:1000*60*5,
httpOnly:true,
};
app.get('/setcookie',(req,res)=>{
res.cookie('cookietwo','cookie may different',options);
res.send(`Cookie has been set...`);
});
app.get('/readcookie',(req,res)=>{
console.log(req.cookies)
res.send(req.cookies.cookietwo);
});Run Code Online (Sandbox Code Playgroud)
我有一个使用“universal-cookie”的 React 组件。在测试用例中调用 mount 时,cookies 对象为空。
import Cookies from 'universal-cookie';
const cookies = new Cookies();
class MyComponent extends React.Component {
componentDidMount(){
axios.defaults.headers.common.Authorization = cookies.get('token').value; //12492525
}
}
//My Test File
describe('MyComponent Test', () => {
it('Component mounted', () => {
const wrapper = mount(<MyComponent/>);
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行测试文件时,我不断收到错误
Cookie 附近的“类型错误:无法读取未定义的属性‘值’”。
无论我尝试模拟或设置多少次,Cookie 值都是空的。
到目前为止我尝试设置
cookies.set('HAS_DOCUMENT_COOKIE', true);
cookies.set('token', { value: '1244'});// in my test file
Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,该应用程序使用 ReactJs 作为localhost:3001 的前端,使用 Symfony 作为后端localhost:3000,并且为了启用跨源通信,我在 Symfony 中使用“cors-bundle”。现在我想在用户登录时创建一个Cookie,但它似乎没有创建!
感谢您的帮助,
这是用户登录时 Symfony 中的代码:
use Symfony\Component\HttpFoundation\Cookie;
$cookie = new Cookie( 'my_cookie', 1, strtotime('tomorrow') );
$res = new Response();
$res->headers->setCookie( $cookie );
return new JsonResponse($res);
Run Code Online (Sandbox Code Playgroud)
这也是我尝试过的:
$res->headers->setCookie(Cookie::create('my_cookie', 1));
return new JsonResponse($res);
Run Code Online (Sandbox Code Playgroud) 我正在将旧站点升级到 ASP.NET Core 2.1,但仍将 .NET Framework 4.7.2 作为目标框架。
我们正在尝试使用 WSFederation 服务来处理身份验证,如本 Microsoft文档中所述。一个单独的站点处理实际的登录,然后重定向回我们位于 /Federation 端点的站点。
我们的 Startup.cs 类:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(authOptions =>
{
authOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(wsOptions =>
{
wsOptions.Wtrealm = Configuration.GetValue<string>("WSFed:Realm");
wsOptions.MetadataAddress = Configuration.GetValue<string>("WSFed:Metadata");
wsOptions.CallbackPath = "/Federation";
})
.AddCookie(cookieOptions =>
{
cookieOptions.Cookie.SameSite = SameSiteMode.None;
});
//Fixes correlation error
services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpsRedirection(options =>
{
options.HttpsPort = 443;
});
}
public void Configure(IApplicationBuilder …Run Code Online (Sandbox Code Playgroud) 我有一个托管在 Heroku 中的 Flask 应用程序,作为 iframe 嵌入到我的网站之一。假设a.com将其呈现<heroku_url>.com为 iframe。当用户访问 时a.com,<heroku_url>.com会被渲染并创建会话。
from flask import session, make_response
@app.route("/")
def index():
session['foo'] = 'bar'
response = make_response("setting cookie")
response.headers.add('Set-Cookie', 'cross-site-cookie=bar; SameSite=None; Secure')
return response
Run Code Online (Sandbox Code Playgroud)
在 Chrome 开发工具中,我看到 cookie 被阻止。不过在 Firefox 中运行良好。我是否正确设置了 cookie?我知道这是由于 chrome80 更新造成的,但不确定解决方法
我正在修改我们的网站 cookie 政策以使其符合要求,但我收到了各种相互矛盾的信息。我也查看了其他大型网站和门户网站,但问题仍然存在:
Cookie 处理是否还包括存储处理?或者我可以将任何我喜欢的东西存储在存储中,而无需征求许可?
我安装了 yangsuite[core],部署它并使用 iframe 调用它
<iframe
width="700"
height="800"
src="https://10.225.61.70:7143/netconf/getyang/hangxin+travelsky-set/ietf-interfaces">
</iframe>
Run Code Online (Sandbox Code Playgroud)
下面是视图函数代码(在lib/python3.8/site-packages/ysnetconf/views/rpc.py目录下):
@login_required
def get_yang(request, yangset=None, modulenames=None):
devices = YSDeviceProfile.list(require_feature="netconf")
replay_dir = get_path('tasks_dir', user=request.user.username)
return render(request, 'ysnetconf/netconf.html', {
'devices': devices,
'yangset': yangset or '',
'modulenames': modulenames or '',
'replay_dir': replay_dir
})
Run Code Online (Sandbox Code Playgroud)
它需要登录,我更改了代码以绕过登录(有一些代码逻辑需要访问request.user)
# @login_required
def get_yang(request, yangset=None, modulenames=None):
devices = YSDeviceProfile.list(require_feature="netconf")
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
user = authenticate(username='admin', password='password')
login(request, user)
replay_dir = get_path('tasks_dir', user=request.user.username)
return render(request, 'ysnetconf/netconf.html', {
'devices': devices,
'yangset': yangset or '',
'modulenames': modulenames …Run Code Online (Sandbox Code Playgroud) 我使用 Go 和 mux 作为后端,使用简单的 html 作为前端。在响应中设置cookie的代码(不完整):
import "github.com/gorilla/sessions" // this is where sessions come from
var store = sessions.NewCookieStore([]byte("secret"))
store.Options = &sessions.Options{
MaxAge: 3600 * 24,
HttpOnly: true,
Path: "/",
Secure: true,
}
session, _ := store.Get(request, "uid")
session.Values["uid"] = token
err = session.Save(request, writer)
if err != nil {
log.Fatalln(err)
return
}
Run Code Online (Sandbox Code Playgroud)
这就是我获取的方式:
fetch("http://localhost:8000/user/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
credentials: 'include',
body: JSON.stringify(user),
})
Run Code Online (Sandbox Code Playgroud)
我还在后端启用了 cors:
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://127.0.0.1:3000"},
AllowCredentials: true,
})
Run Code Online (Sandbox Code Playgroud)
set-cookie …