I have website MAIN (http://localhost:63085/) with an iframe embedded in it, the iframe source is from another host website BRANCH (http://localhost:50725/). The page inside the iframe needs cookies to work (because it uses forms authentication).
I set an API on branch website that authenticates the user and sends a cookie back in the response. The Main website will call the API, then load the default page (hosted under branch) in iframe.
The default page needs the form authentication cookie. I supposed that the iframe should read the cookie automatically (since they are both under localhost). But, the user in the default page was not authenticated and never reads the cookie.
This is the Ajax function from Main Branch Site:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="row">
<div class="row">
<iframe id="frmCompas" width="800" height="800"></iframe>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
console.log("ready!");
authUser();
});
function authUser() {
$.ajax({
url: 'http://localhost:50725/api/auth?name=h&pass=1',
type: 'POST',
dataType: 'json',
success: function (data) { //auth user through the web api to generate an authentication Cookie
frmCompas.src = "http://localhost:50725/Default.aspx"
},
error: function () {
console.log('Error in Operation');
}
});
}
</script>
As you see, after calling API (http://localhost:50725) under branch, I load the default page in the iframe.
My problem is that when calling the Main Site, the cookie returned is not read and sent with with request when rendering Default Page:
Testing 1st Case: Calling Main Site
- Call to Auth API From Main Site
I also tried to the browse the branch site default page directly in url (it also didn't get that cookie) and didn't get authenticated.
Test 2nd Case: Calling Auth Api in Seperate URL
The API: returned the cookie: "36FED3D72417.."
Now, when I browse the main web site, here what happens when checking the traffic:
- Calling Localhost: the cookie is associated in the request from the begining
- Calling API:
- Calling Main/Default.aspx in Iframe:
Finding: The iframe reads the cookie which was generated by the api when called outside main site.
How to resolve this issue and make the cookie readable by the iframe in the 1st test case?