Cross-Domain Cookies

后端 未结 15 2523
抹茶落季
抹茶落季 2020-11-21 21:56

I have two webapps WebApp1 and WebApp2 in two different domains.

  1. I am setting a cookie in WebApp1 in the HttpResponse.
  2. How to read the same cookie fro
相关标签:
15条回答
  • 2020-11-21 22:41

    You can attempt to push the cookie val to another domain using an image tag.

    Your mileage may vary when trying to do this because some browsers require you to have a proper P3P Policy on the WebApp2 domain or the browser will reject the cookie.

    If you look at plus.google.com p3p policy you will see that their policy is:

    CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."

    that is the policy they use for their +1 buttons to these cross domain requests.

    Another warning is that if you are on https make sure that the image tag is pointing to an https address also otherwise the cookies will not set.

    0 讨论(0)
  • 2020-11-21 22:41

    One can use invisible iframes to get the cookies. Let's say there are two domains, a.com and b.com. For the index.html of domain a.com one can add (notice height=0 width=0):

    <iframe height="0" id="iframe" src="http://b.com" width="0"></iframe>
    

    That way your website will get b.com cookies assuming that http://b.com sets the cookies.

    The next thing would be manipulating the site inside the iframe through JavaScript. The operations inside iframe may become a challenge if one doesn't own the second domain. But in case of having access to both domains referring the right web page at the src of iframe should give the cookies one would like to get.

    0 讨论(0)
  • 2020-11-21 22:42

    There's no such thing as cross domain cookies. You could share a cookie between foo.example.com and bar.example.com but never between example.com and example2.com and that's for security reasons.

    0 讨论(0)
  • 2020-11-21 22:42
    function GetOrder(status, filter) {
        var isValid = true; //isValidGuid(customerId);
        if (isValid) {
            var refundhtmlstr = '';
            //varsURL = ApiPath + '/api/Orders/Customer/' + customerId + '?status=' + status + '&filter=' + filter;
            varsURL = ApiPath + '/api/Orders/Customer?status=' + status + '&filter=' + filter;
            $.ajax({
                type: "GET",
                //url: ApiPath + '/api/Orders/Customer/' + customerId + '?status=' + status + '&filter=' + filter,
                url: ApiPath + '/api/Orders/Customer?status=' + status + '&filter=' + filter,
                dataType: "json",
                crossDomain: true,
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    var htmlStr = '';
                    if (data == null || data.Count === 0) {
                        htmlStr = '<div class="card"><div class="card-header">Bu kriterlere uygun sipariş bulunamadı.</div></div>';
                    }
                    else {
                        $('#ReturnPolicyBtnUrl').attr('href', data.ReturnPolicyBtnUrl);
                        var groupedData = data.OrderDto.sort(function (x, y) {
                            return new Date(y.OrderDate) - new Date(x.OrderDate);
                        });
                        groupedData = _.groupBy(data.OrderDto, function (d) { return toMonthStr(d.OrderDate) });
                        localStorage['orderData'] = JSON.stringify(data.OrderDto);
    
                        $.each(groupedData, function (key, val) {
    
                            var sortedData = groupedData[key].sort(function (x, y) {
                                return new Date(y.OrderDate) - new Date(x.OrderDate);
                            });
                            htmlStr += '<div class="card-header">' + key + '</div>';
                            $.each(sortedData, function (keyitem, valitem) {
                                //Date Convertions
                                if (valitem.StatusDesc != null) {
                                    valitem.StatusDesc = valitem.StatusDesc;
                                }
    
                                var date = valitem.OrderDate;
                                date = date.substring(0, 10).split('-');
                                date = date[2] + '.' + date[1] + '.' + date[0];
                                htmlStr += '<div class="col-lg-12 col-md-12 col-xs-12 col-sm-12 card-item clearfix ">' +
                            //'<div class="card-item-head"><span class="order-head">Sipariş No: <a href="ViewOrderDetails.html?CustomerId=' + customerId + '&OrderNo=' + valitem.OrderNumber + '" >' + valitem.OrderNumber + '</a></span><span class="order-date">' + date + '</span></div>' +
                            '<div class="card-item-head"><span class="order-head">Sipariş No: <a href="ViewOrderDetails.html?OrderNo=' + valitem.OrderNumber + '" >' + valitem.OrderNumber + '</a></span><span class="order-date">' + date + '</span></div>' +
                            '<div class="card-item-head-desc">' + valitem.StatusDesc + '</div>' +
                            '<div class="card-item-body">' +
                                '<div class="slider responsive">';
                                var i = 0;
                                $.each(valitem.ItemList, function (keylineitem, vallineitem) {
                                    var imageUrl = vallineitem.ProductImageUrl.replace('{size}', 200);
                                    htmlStr += '<div><img src="' + imageUrl + '" alt="' + vallineitem.ProductName + '"><span class="img-desc">' + ProductNameStr(vallineitem.ProductName) + '</span></div>';
                                    i++;
                                });
                                htmlStr += '</div>' +
                            '</div>' +
                        '</div>';
                            });
                        });
    
                        $.each(data.OrderDto, function (key, value) {
                            if (value.IsSAPMigrationflag === true) {
                                refundhtmlstr = '<div class="notify-reason"><span class="note"><B>Notification : </B> Geçmiş siparişleriniz yükleniyor.  Lütfen kısa bir süre sonra tekrar kontrol ediniz. Teşekkürler. </span></div>';
                            }
                        });
                    }
                    $('#orders').html(htmlStr);
                    $("#notification").html(refundhtmlstr);
                    ApplySlide();
                },
                error: function () {
                    console.log("System Failure");
                }
            });
        }
    }
    

    Web.config

    Include UI origin and set Allow Crentials to true

    <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="http://burada.com" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
            <add name="Access-Control-Allow-Credentials" value="true" />
          </customHeaders>
        </httpProtocol>
    
    0 讨论(0)
  • 2020-11-21 22:47

    Do what Google is doing. Create a PHP file that sets the cookie on all 3 domains. Then on the domain where the theme is going to set, create a HTML file that would load the PHP file that sets cookie on the other 2 domains. Example:

    <html>
       <head></head>
       <body>
          <p>Please wait.....</p>
          <img src="http://domain2.com/setcookie.php?theme=whateveryourthemehere" />
          <img src="http://domain3.com/setcookie.php?theme=whateveryourthemehere" />
       </body>
    </html>
    

    Then add an onload callback on body tag. The document will only load when the images completely load that is when cookies are set on the other 2 domains. Onload Callback :

    <head>
       <script>
       function loadComplete(){
          window.location="http://domain1.com";//URL of domain1
       }
       </script>
    </head>
    <body onload="loadComplete()">
    
    setcookie.php
    

    We set the cookies on the other domains using a PHP file like this :

    <?php
    if(isset($_GET['theme'])){
       setcookie("theme", $_GET['theme'], time()+3600);
    }
    ?>
    

    Now cookies are set on the three domains.

    0 讨论(0)
  • 2020-11-21 22:47

    Since it is difficult to do 3rd party cookies and also some browsers won't allow that.

    You can try storing them in HTML5 local storage and then sending them with every request from your front end app.

    0 讨论(0)
提交回复
热议问题