<!DOCTYPE html>
<html xmlns="www.w3.org/.../xhtml">
<head runat="server">
<title></title>
<script src="ajax.googleapis.com/.../script>
<script type="text/javascript">
$(document).ready(function () {
debugger;
login(<office 365>, '<pwd>', <office 365 sit>/_forms/default.aspx?wa=wsignin1.0', function () {
$.ajax({
url: <office site>/sites/Sitename/_api/web/title',
dataType: 'text',
type: 'GET',
success: function (result, textStatus, jqXHR) {
$('#results').text(result);
}
});
}, function () {
//$('#results').text('Result: failed');
alert('fail :(')
});
});
function login(userID, password, url, successBlock, failBlock) {
$.support.cors = true;
$.ajax({
url: 'login.microsoftonline.com/extSTS.srf',
dataType: 'text',
type: 'POST',
crossDomain: true,
data: getSAMLRequest(userID, password, url),
headers: {
Accept: "application/soap+xml; charset=utf-8"
},
success: function (result, textStatus, jqXHR) {
var xmlDoc = $.parseXML(result);
var xml = $(xmlDoc)
var securityToken = xml.find("BinarySecurityToken").text();
$('#results').text(result);
alert(result);
alert(textStatus);
alert(jqXHR);
if (securityToken.length == 0) {
alert('came here');
failBlock();
}
else {
$.ajax({
url: url,
dataType: 'text',
type: 'POST',
data: xml.find("BinarySecurityToken").text(),
headers: {
Accept: "application/x-www-form-urlencoded"
},
success: function (result, textStatus, jqXHR) {
successBlock();
},
error: function (jqXHR, textStatus, errorThrown) {
failBlock();
}
});
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert('jqXHR:' + jqXHR + '***textStatus:' + textStatus + '***errorThrown:' + errorThrown);
failBlock();
}
});
}
function getSAMLRequest(userID, password, url) {
return '<s:Envelope'
+ 'xmlns:s="www.w3.org/.../soap-envelope"'
+ 'xmlns:a="www.w3.org/.../addressing" '
+ 'xmlns:u="docs.oasis-open.org/.../oasis-200401-wss-wssecurity-utility-1.0.xsd"> '
+ '<s:Header> '
+ '<a:Action s:mustUnderstand="1">schemas.xmlsoap.org/.../a:Action> '
+ '<a:ReplyTo> '
+ '<a:Address>www.w3.org/.../a:Address> '
+ '</a:ReplyTo> '
+ '<a:To s:mustUnderstand="1">login.microsoftonline.com/.../a:To> '
+ '<o:Security '
+ 's:mustUnderstand="1" '
+ 'xmlns:o="docs.oasis-open.org/.../oasis-200401-wss-wssecurity-secext-1.0.xsd"> '
+ '<o:UsernameToken> '
+ '<o:Username>' + userID + '</o:Username> '
+ '<o:Password>' + password + '</o:Password> '
+ '</o:UsernameToken> '
+ '</o:Security> '
+ '</s:Header> '
+ '<s:Body> '
+ '<t:RequestSecurityToken xmlns:t="schemas.xmlsoap.org/.../trust"> '
+ '<wsp:AppliesTo xmlns:wsp="schemas.xmlsoap.org/.../policy"> '
+ '<a:EndpointReference> '
+ '<a:Address>' + url + '</a:Address> '
+ '</a:EndpointReference> '
+ '</wsp:AppliesTo> '
+ '<t:KeyType>schemas.xmlsoap.org/.../t:KeyType> '
+ '<t:RequestType>schemas.xmlsoap.org/.../t:RequestType> '
+ '<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>'
+ '</t:RequestSecurityToken>'
+ '</s:Body> '
+ '</s:Envelope>';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="results">
</div>
</form>
</body>
</html>
but irrespective of passing user id and password
it returns invalid STS token , any clues?
<S:Detail><psf:error><psf:value>0x80048820</psf:value><psf:internalerror><psf:code>0x80045c01</psf:code><psf:text>Invalid STS request.
</psf:text></psf:internalerror></psf:error></S:Detail></S:Fault></S:Body></S:Envelope>