Beispiel #1
0
        /// <summary>
        /// Function that sends the token request to Windows Live.
        /// </summary>
        /// <param name="uriForTokenEndpointReference">The Uri to use for the endpoint reference for our token</param>
        /// <returns>Response to token request.</returns>
        private HttpWebResponse EmitTokenRequest(Uri uriForTokenEndpointReference)
        {
            const string TokenRequest =
                "<?xml version='1.0' encoding='UTF-8'?>" +
                "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope' " +
                "            xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' " +
                "            xmlns:saml='urn:oasis:names:tc:SAML:1.0:assertion' " +
                "            xmlns:wsp='http://schemas.xmlsoap.org/ws/2004/09/policy' " +
                "            xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' " +
                "            xmlns:wsa='http://www.w3.org/2005/08/addressing' " +
                "            xmlns:wssc='http://schemas.xmlsoap.org/ws/2005/02/sc' " +
                "            xmlns:wst='http://schemas.xmlsoap.org/ws/2005/02/trust' " +
                "            xmlns:ps='http://schemas.microsoft.com/Passport/SoapServices/PPCRL'>" +
                "  <s:Header>" +
                "    <wsa:Action s:mustUnderstand='1'>http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</wsa:Action>" +
                "    <wsa:To s:mustUnderstand='1'>{0}</wsa:To>" +
                "    <ps:AuthInfo Id='PPAuthInfo'>" +
                "      <ps:HostingApp>{{63f179af-8bcd-49a0-a3e5-1154c02df090}}</ps:HostingApp>" + //// NOTE: I generated a new GUID for the EWS API
                "      <ps:BinaryVersion>5</ps:BinaryVersion>" +
                "      <ps:UIVersion>1</ps:UIVersion>" +
                "      <ps:Cookies></ps:Cookies>" +
                "      <ps:RequestParams>AQAAAAIAAABsYwQAAAAxMDMz</ps:RequestParams>" +
                "    </ps:AuthInfo>" +
                "    <wsse:Security>" +
                "      <wsse:UsernameToken wsu:Id='user'>" +
                "        <wsse:Username>{1}</wsse:Username>" +
                "        <wsse:Password>{2}</wsse:Password>" +
                "      </wsse:UsernameToken>" +
                "      <wsu:Timestamp Id='Timestamp'>" +
                "        <wsu:Created>{3}</wsu:Created>" +
                "        <wsu:Expires>{4}</wsu:Expires>" +
                "      </wsu:Timestamp>" +
                "    </wsse:Security>" +
                "  </s:Header>" +
                "  <s:Body>" +
                "    <ps:RequestMultipleSecurityTokens Id='RSTS'>" +
                "      <wst:RequestSecurityToken Id='RST0'>" +
                "        <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>" +
                "        <wsp:AppliesTo>" +
                "          <wsa:EndpointReference>" +
                "            <wsa:Address>http://Passport.NET/tb</wsa:Address>" +
                "          </wsa:EndpointReference>" +
                "        </wsp:AppliesTo>" +
                "      </wst:RequestSecurityToken>" +
                "      <wst:RequestSecurityToken Id='RST1'>" +
                "        <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>" +
                "        <wsp:AppliesTo>" +
                "          <wsa:EndpointReference>" +
                "            <wsa:Address>{5}</wsa:Address>" +
                "          </wsa:EndpointReference>" +
                "        </wsp:AppliesTo>" +
                "        <wsp:PolicyReference URI='LBI_FED_SSL'></wsp:PolicyReference>" +
                "      </wst:RequestSecurityToken>" +
                "    </ps:RequestMultipleSecurityTokens>" +
                "  </s:Body>" +
                "</s:Envelope>";

            // Create a security timestamp valid for 5 minutes to send with the request.
            DateTime          now = DateTime.UtcNow;
            SecurityTimestamp securityTimestamp = new SecurityTimestamp(now, now.AddMinutes(5), "Timestamp");

            // Format the request string to send to the server, filling in all the bits.
            string requestToSend = String.Format(
                TokenRequest,
                this.windowsLiveUrl,
                this.windowsLiveId,
                this.password,
                securityTimestamp.GetCreationTimeChars(),
                securityTimestamp.GetExpiryTimeChars(),
                uriForTokenEndpointReference.ToString());

            // Create and send the request.
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(this.windowsLiveUrl);

            webRequest.Method      = "POST";
            webRequest.ContentType = "text/xml; charset=utf-8";
            byte[] requestBytes = Encoding.UTF8.GetBytes(requestToSend);
            webRequest.ContentLength = requestBytes.Length;

            // NOTE: We're not tracing the request to Windows Live here because it has the user name and
            // password in it.
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                requestStream.Write(requestBytes, 0, requestBytes.Length);
            }

            return((HttpWebResponse)webRequest.GetResponse());
        }
        /// <summary>
        /// Function that sends the token request to Windows Live.
        /// </summary>
        /// <param name="uriForTokenEndpointReference">The Uri to use for the endpoint reference for our token</param>
        /// <returns>Response to token request.</returns>
        private HttpWebResponse EmitTokenRequest(Uri uriForTokenEndpointReference)
        {
            const string TokenRequest =
                "<?xml version='1.0' encoding='UTF-8'?>" +
                "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope' " +
                "            xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' " +
                "            xmlns:saml='urn:oasis:names:tc:SAML:1.0:assertion' " +
                "            xmlns:wsp='http://schemas.xmlsoap.org/ws/2004/09/policy' " +
                "            xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' " +
                "            xmlns:wsa='http://www.w3.org/2005/08/addressing' " +
                "            xmlns:wssc='http://schemas.xmlsoap.org/ws/2005/02/sc' " +
                "            xmlns:wst='http://schemas.xmlsoap.org/ws/2005/02/trust' " +
                "            xmlns:ps='http://schemas.microsoft.com/Passport/SoapServices/PPCRL'>" +
                "  <s:Header>" +
                "    <wsa:Action s:mustUnderstand='1'>http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</wsa:Action>" +
                "    <wsa:To s:mustUnderstand='1'>{0}</wsa:To>" +
                "    <ps:AuthInfo Id='PPAuthInfo'>" +
                "      <ps:HostingApp>{{63f179af-8bcd-49a0-a3e5-1154c02df090}}</ps:HostingApp>" + //// NOTE: I generated a new GUID for the EWS API
                "      <ps:BinaryVersion>5</ps:BinaryVersion>" +
                "      <ps:UIVersion>1</ps:UIVersion>" +
                "      <ps:Cookies></ps:Cookies>" +
                "      <ps:RequestParams>AQAAAAIAAABsYwQAAAAxMDMz</ps:RequestParams>" +
                "    </ps:AuthInfo>" +
                "    <wsse:Security>" +
                "      <wsse:UsernameToken wsu:Id='user'>" +
                "        <wsse:Username>{1}</wsse:Username>" +
                "        <wsse:Password>{2}</wsse:Password>" +
                "      </wsse:UsernameToken>" +
                "      <wsu:Timestamp Id='Timestamp'>" +
                "        <wsu:Created>{3}</wsu:Created>" +
                "        <wsu:Expires>{4}</wsu:Expires>" +
                "      </wsu:Timestamp>" +
                "    </wsse:Security>" +
                "  </s:Header>" +
                "  <s:Body>" +
                "    <ps:RequestMultipleSecurityTokens Id='RSTS'>" +
                "      <wst:RequestSecurityToken Id='RST0'>" +
                "        <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>" +
                "        <wsp:AppliesTo>" +
                "          <wsa:EndpointReference>" +
                "            <wsa:Address>http://Passport.NET/tb</wsa:Address>" +
                "          </wsa:EndpointReference>" +
                "        </wsp:AppliesTo>" +
                "      </wst:RequestSecurityToken>" +
                "      <wst:RequestSecurityToken Id='RST1'>" +
                "        <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>" +
                "        <wsp:AppliesTo>" +
                "          <wsa:EndpointReference>" +
                "            <wsa:Address>{5}</wsa:Address>" +
                "          </wsa:EndpointReference>" +
                "        </wsp:AppliesTo>" +
                "        <wsp:PolicyReference URI='LBI_FED_SSL'></wsp:PolicyReference>" +
                "      </wst:RequestSecurityToken>" +
                "    </ps:RequestMultipleSecurityTokens>" +
                "  </s:Body>" +
                "</s:Envelope>";
            
            // Create a security timestamp valid for 5 minutes to send with the request.
            DateTime now = DateTime.UtcNow;
            SecurityTimestamp securityTimestamp = new SecurityTimestamp(now, now.AddMinutes(5), "Timestamp");

            // Format the request string to send to the server, filling in all the bits.
            string requestToSend = String.Format(
                TokenRequest,
                this.windowsLiveUrl,
                this.windowsLiveId,
                this.password,
                securityTimestamp.GetCreationTimeChars(),
                securityTimestamp.GetExpiryTimeChars(),
                uriForTokenEndpointReference.ToString());

            // Create and send the request.
            HttpWebRequest webRequest = (HttpWebRequest) HttpWebRequest.Create(this.windowsLiveUrl);
            
            webRequest.Method = "POST";
            webRequest.ContentType = "text/xml; charset=utf-8";
            byte[] requestBytes = Encoding.UTF8.GetBytes(requestToSend);
            webRequest.ContentLength = requestBytes.Length;

            // NOTE: We're not tracing the request to Windows Live here because it has the user name and
            // password in it.
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                requestStream.Write(requestBytes, 0, requestBytes.Length);
            }
            
            return (HttpWebResponse)webRequest.GetResponse();
        }