Javascript 试图在 cookie 中保存用户名/密码;查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8508999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to save username / password in cookie ; JQuery
提问by JFFF
I saw this example about storing login information in cookie.
我看到了这个关于在 cookie 中存储登录信息的例子。
http://eisabainyo.net/weblog/2009/02/20/store-login-information-in-cookie-using-jquery/
http://eisabainyo.net/weblog/2009/02/20/store-login-information-in-cookie-using-jquery/
Tried it, but can't seem to get it to work. I have to plugin and I don't see what's wrong here.
试过了,但似乎无法让它工作。我必须插入,但我看不出这里有什么问题。
This page is Login.aspx which points to AccountMenu.aspx
这个页面是指向 AccountMenu.aspx 的 Login.aspx
EDITIT'S FOR A DEMO, TEST, WHATEVER YOU WANNA CALL IT. this site will never go online. I KNOW this is not the way to do it. I'm looking for help to solve my problem, not people telling me this is bad design.
编辑它是为了演示、测试,无论你想怎么称呼它。这个网站永远不会上线。我知道这不是这样做的方法。我正在寻求帮助来解决我的问题,而不是人们告诉我这是糟糕的设计。
(...)
(……)
<div data-role="content">
<form action="AccountMenu.aspx" method="post" id="login">
<label for="email">Email</label>
<input type="text" id="email" name="email"/>
<label for="password"><%= GetLabel("password") %></label>
<input id="password" type="password" name="password" />
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<input type="checkbox" name="remember" id="remember" class="custom" checked="true" />
<label for="remember">Remember me ?</label>
</fieldset>
</div>
<input type="hidden" name="submit" value="submitted" />
<button type="submit" name="submit" id="submit" data-theme="a" value="<%= GetLabel("new-login") %>" ></button>
<button type="button" disabled="disabled" data-theme="a" value="<%= GetLabel("new-account") %>"></button>
</div>
</form>
<script type="text/javascript">
if ($('#remember').attr('checked'))
{
var email = $('#email').attr("value");
var password = $('#password').attr("value");
// set cookies to expire in 14 days
$.cookie('email', email, { expires: 14 });
$.cookie('password', password, { expires: 14 });
$.cookie('remember', true, { expires: 14 });
}
else
{
// reset cookies
$.cookie('email', null);
$.cookie('password', null);
$.cookie('remember', null);
}
var remember = $.cookie('remember');
if (remember == 'true')
{
var email = $.cookie('email');
var password = $.cookie('password');
// autofill the fields
$('#email').attr("value", email);
$('#password').attr("value", password);
}
</script>
回答by mplungjan
you need to actually call the code when the user fills in the form
您需要在用户填写表单时实际调用代码
$(document).ready(function() {
var remember = $.cookie('remember');
if (remember == 'true')
{
var email = $.cookie('email');
var password = $.cookie('password');
// autofill the fields
$('#email').val(email);
$('#password').val(password);
}
$("#login").submit(function() {
if ($('#remember').is(':checked')) {
var email = $('#email').val();
var password = $('#password').val();
// set cookies to expire in 14 days
$.cookie('email', email, { expires: 14 });
$.cookie('password', password, { expires: 14 });
$.cookie('remember', true, { expires: 14 });
}
else
{
// reset cookies
$.cookie('email', null);
$.cookie('password', null);
$.cookie('remember', null);
}
});
});
回答by austincheney
document.cookie = "login=" + username_from_DOM_here + "-----" + password_from_DOM_here + "; secure";
Do not use a path in the cookie declaration and ensure that the page is HTTPS then you should be just fine. Secure cookies can only be transmitted on HTTPS pages and ignoring the path qualifier ensures the cookie is not available to other parts of the site. Also do not use an expires qualifier either, because the cookie will expire the moment the browser session ends.
不要在 cookie 声明中使用路径并确保页面是 HTTPS,那么你应该没问题。安全 cookie 只能在 HTTPS 页面上传输,忽略路径限定符可确保 cookie 不可用于站点的其他部分。也不要使用 expires 限定符,因为 cookie 将在浏览器会话结束时过期。
There are some remaining security concerns in that the cookie still contains unhashed credentials in a text file. Even though that cookie should never be transmitted outside of HTTPS it can still be accessed by malware outside the browser.
还有一些安全问题,因为 cookie 仍然在文本文件中包含未散列的凭据。即使该 cookie 永远不应该在 HTTPS 之外传输,它仍然可以被浏览器之外的恶意软件访问。
回答by Soumen
**At first you need jquery.cookie.js file**
<script type="text/javascript">
$(document).ready(function() {
var remember = $.cookie('remember');
if (remember == 'true')
{
var username = $.cookie('username');
var password = $.cookie('password');
// autofill the fields
$('#username').val(username);
$('#password').val(password);
$('#login-check').attr('checked',true);//#login-check checkbox id..
}
$("#UserLoginForm").submit(function() {
if ($('#login-check').is(':checked')) {
var username = $('#username').val();
var password = $('#password').val();
// set cookies to expire in 14 days
$.cookie('username', username, { expires: 14 });
$.cookie('password', password, { expires: 14 });
$.cookie('remember', true, { expires: 14 });
}
else
{
// reset cookies
$.cookie('username', null);
$.cookie('password', null);
$.cookie('remember', null);
}
});
});
</script>