TicketAcquirer.cs.svn-base
上传用户:asz878
上传日期:2020-04-01
资源大小:189k
文件大小:4k
源码类别:

WEB邮件程序

开发平台:

Visual C++

  1. // Code by MSDN 
  2. // http://msdn2.microsoft.com/en-us/library/bb447721.aspx
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Xml;
  7. namespace OpenContactsNet
  8. {
  9.     internal class TicketAcquirer
  10.     {
  11.         private const string applicationId = "10"; // An arbitrary value that will be defined in the next non-alpha release
  12.         public string GetTicket( NetworkCredential credentail )
  13.         {
  14.             string soapEnvelope =
  15.                 @"<s:Envelope
  16.     xmlns:s = ""http://www.w3.org/2003/05/soap-envelope""
  17.     xmlns:wsse = ""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""
  18.     xmlns:saml = ""urn:oasis:names:tc:SAML:1.0:assertion""
  19.     xmlns:wsp = ""http://schemas.xmlsoap.org/ws/2004/09/policy""
  20.     xmlns:wsu = ""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""
  21.     xmlns:wsa = ""http://www.w3.org/2005/08/addressing""
  22.     xmlns:wssc = ""http://schemas.xmlsoap.org/ws/2005/02/sc""
  23.     xmlns:wst = ""http://schemas.xmlsoap.org/ws/2005/02/trust"">
  24.     <s:Header>
  25.         <wlid:ClientInfo xmlns:wlid = ""http://schemas.microsoft.com/wlid"">
  26.             <wlid:ApplicationID>" + applicationId + @"</wlid:ApplicationID>
  27.         </wlid:ClientInfo>
  28.         <wsa:Action s:mustUnderstand = ""1"">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</wsa:Action>
  29.         <wsa:To s:mustUnderstand = ""1"">https://dev.login.live.com/wstlogin.srf</wsa:To>
  30.         <wsse:Security>
  31.             <wsse:UsernameToken wsu:Id = ""user"">
  32.                 <wsse:Username>" + credentail.UserName + @"</wsse:Username>
  33.                 <wsse:Password>" + credentail.Password + @"</wsse:Password>
  34.             </wsse:UsernameToken>
  35.         </wsse:Security>
  36.     </s:Header>
  37.     <s:Body>
  38.         <wst:RequestSecurityToken Id = ""RST0"">
  39.             <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>
  40.             <wsp:AppliesTo>
  41.                 <wsa:EndpointReference>
  42.                     <wsa:Address>http://live.com</wsa:Address>
  43.                 </wsa:EndpointReference>
  44.             </wsp:AppliesTo>
  45.             <wsp:PolicyReference URI = ""MBI""></wsp:PolicyReference>
  46.         </wst:RequestSecurityToken>
  47.     </s:Body>
  48. </s:Envelope>";
  49.             const string url = @"https://dev.login.live.com/wstlogin.srf";
  50.             WebRequest request = WebRequest.Create( url );
  51.             request.Method = "POST";
  52.             request.ContentType = "application/soap+xml; charset=UTF-8";
  53.             request.Timeout = 10 * 1000; // Wait for at most 10 seconds
  54.             byte[] bytes = Encoding.UTF8.GetBytes( soapEnvelope );
  55.             request.GetRequestStream().Write( bytes, 0, bytes.Length );
  56.             request.GetRequestStream().Close();
  57.             WebResponse response;
  58.             response = request.GetResponse();
  59.             string xml;
  60.             using ( StreamReader reader = new StreamReader( response.GetResponseStream() ) )
  61.             {
  62.                 xml = reader.ReadToEnd();
  63.             }
  64.             response.Close();
  65.             XmlDocument document = new XmlDocument();
  66.             document.LoadXml( xml );
  67.             XmlNamespaceManager nsManager = new XmlNamespaceManager( document.NameTable );
  68.             nsManager.AddNamespace( "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" );
  69.             XmlNode node = document.SelectSingleNode( @"//wsse:BinarySecurityToken/text()", nsManager );
  70.             if ( node == null )
  71.             {
  72.                 return null; // The wsse:BinarySecurityToken element is missing. Examine the xml for error information
  73.             }
  74.             else
  75.             {
  76.                 return node.Value;
  77.             }
  78.         }
  79.     }
  80. }