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

WEB邮件程序

开发平台:

Visual C++

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