javascript - Chrome 扩展和 Django : Verify whether third party cookies are allowed

标签 javascript django cookies google-chrome-extension xmlhttprequest

我正在使用 Django 的 cookie 身份验证。

我有一个 Chrome 扩展程序。当用户单击扩展按钮时,扩展会向 my_site.com/cookie_test/发送 xmlHttp 请求

附加到此 url 的 View 让用户知道他是否登录到 my_site.com。 (它也做其他事情。)

Content_script.js

function got_response(){ 
    if (xmlHttp.readyState == 4){
        alert(xmlHttp.responseText);  
    }
 }

var xmlHttp=new XMLHttpRequest();
xmlHttp.open("POST", "my_site.com/cookie_test/",true);
xmlHttp.onreadystatechange = got_response;
xmlHttp.send();

查看my_site.com/cookie_test/调用的函数

@csrf_exempt
def cookie_test(request):
    if not request.user.is_authenticated():
        return HttpResponse("not logged in")
    else:
        return HttpResponse("logged in")

情况 1:用户登录到 my_site 并且浏览器允许第三方 cookie

结果:“登录”正确

情况 2:用户未登录

结果:“未登录”正确

情况 3:用户已登录,但浏览器不允许第三方 cookie

结果:“未登录”不正确

我的问题

如果用户的浏览器不允许第三方 cookie,我该如何显示消息“更改您的 cookie 设置!”换句话说,我如何区分未登录和不允许第三方 cookie 之间的区别?这可以是服务器端或客户端。

最佳答案

我最终做了什么:

总结: 如果没有 cookie,则在第一次调用时执行 request.session.set_test_cookie()。在这种情况下,以“潜在的 cookie 问题”作为回应。在 content_script 中,如果得到此响应,则进行第二次调用并使用 request.session.test_cookie_worked() 测试是否存在 cookie。

查看第一次调用调用的函数

if not request.user.is_authenticated():
    if len(request.COOKIES) == 0: 
        # User does not allow third party cookies or cookies are deleted/expired
        request.session.set_test_cookie() # Django function
        return HttpResponse("potential cookie issue")
    else:
        return HttpResponse("not logged in") 
else:
    return HttpResponse("logged in") 
    # would actually continue and do stuff here instead

查看第二次调用调用的函数

if not request.session.test_cookie_worked(): # Django function
    return HttpResponse("third party cookies not allowed")      
else:
    return HttpResponse("not logged in") 

content_script.js

function got_response(){ 
    if (xmlHttp.readyState == 4){
        if (response_data == "potential cookie issue"){ 
                function got_response2(){ 
                   if (xmlHttp2.readyState == 4){
                        display_response(xmlHttp2.responseText); 
                       /*either not logged in or cookie issue depending on response*/
                    }
                }  
                var xmlHttp2=new XMLHttpRequest();
                xmlHttp2.open("GET", domain+"/second_call/",true);
                xmlHttp2.onreadystatechange = got_response2;
                xmlHttp2.send();
       }
       else{/* not logged in or logged in depending on response*/}
    }
 }

var xmlHttp=new XMLHttpRequest();
xmlHttp.open("POST", "my_site.com/first_call/",true);
xmlHttp.onreadystatechange = got_response;
xmlHttp.send();

关于javascript - Chrome 扩展和 Django : Verify whether third party cookies are allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311472/

相关文章:

javascript - 在 React 中单击另一个组件中的按钮后如何显示模态?

javascript - 如何将 javascript 函数的值传递给 Django View ?

python - 需要有关使用 Django/DRF 时如何处理和/或避免循环导入的架构建议

javascript - 使用sessionStore和webSockets,还需要cookie吗?

c# - 在 asp.net 中重新加载页面期间 Cookie 被清除

带有代理配置的 angular ng serve 不会从 laravel API 设置 cookie

javascript - ReactJS 没有选择 css 样式

javascript - 突出显示文本区域中的搜索文本

javascript - 用于 iPhone/平板电脑的 HTML 导航菜单

python - 安装 django-haystack