Trying to save username / password in cookie ; JQu

2020-07-10 03:23发布

问题:

I saw this example about storing login information in cookie.

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

EDIT IT'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>    

回答1:

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);
        }
  });
});


回答2:

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.

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.



回答3:

**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>