2012-10-17 1 views
1

asmx 웹 서비스와 테스트 콘솔 앱이 있습니다. 나는이UserNameToken을 ASMX 서비스에 전달하는 방법은 무엇입니까?

private bool IsUserNameTokenPresent() 
    { 
     //Get current SOAP context 
     SoapContext ctxt = RequestSoapContext.Current; 
     UsernameToken user = null; 
     if (ctxt == null) 
     { 
      //This request is using a different protocol other than SOAP. 
      return false; 
     } 

     //Iterate through all Security tokens 
     foreach(SecurityToken tok in ctxt.Security.Tokens) 
     { 
      if (tok is UsernameToken) 
      { 
       user = (UsernameToken)tok; 

      } 
     } 
     if (user == null) 
      return false; 

     return true; 
    } 

질문과 같은 유효성 검사가 웹 서비스 콘솔 응용 프로그램을 참조하여 모든 웹 서비스 호출에이

Employee.Employee e = new TestService.Employee.Employee(); 
e.SomeMethod(); 

처럼 호출을 추가 한 : 어떻게 할 이 서비스를 테스트 할 수 있도록 보안 토큰을 전달하십시오. 항상 null입니다.

답변

2

마지막으로 이에 대한 답변을 찾았습니다. 내 자신의 SOAP 헤더를 수동으로 만들고 요청과 함께 전달해야했습니다. 여기에 몇 가지 코드가 있습니다. 나는 모든 호출에 대해 Nonce를 동적으로 생성해야만했습니다. 누군가 그 코드를 원한다면 여기에 게시 할 것입니다.

  XmlDocument doc = new XmlDocument(); 

      doc.InnerXml = @"<?xml version='1.0' encoding='utf-8'?> 
           <soap:Envelope 
           xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' 
           xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
           xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'> 
           <soap:Header> 
           <wsse:Security soap:mustUnderstand='1'> 
           <wsse:UsernameToken wsu:Id='uuid_faf0159a-6b13-4139-a6da-cb7b4100c10c'> 
           <wsse:Username>UserID</wsse:Username> 
           <wsse:Password>Pass</wsse:Password> 
           <wsse:Nonce>" + nonce + @"</wsse:Nonce> 
           <wsu:Created>" + date + @"</wsu:Created> 
           </wsse:UsernameToken> 
           </wsse:Security> 
           </soap:Header> 
           <soap:Body> 
           <FindBySelfId> 
           <specification> 
           <LastName>" + lastname + @"</LastName> 
           <FirstName>" + firstname + @"</FirstName> 
           <DateOfBirth>" + dob + @"</DateOfBirth> 
           <HomeZipCode>" + zip + @"</HomeZipCode> 
           <SSN4>" + ssn + @"</SSN4> 
           </specification> 
           </FindBySelfId > 
           </soap:Body> 
           </soap:Envelope>"; 

      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/Employee/employee.asmx"); 

      req.Headers.Add("SOAPAction", "https://<Namespace here>"); 

      req.ContentType = "text/xml;charset=\"utf-8\""; 
      req.Accept = "text/xml"; 

      req.Method = "POST"; 
      Stream stm = req.GetRequestStream(); 
      doc.Save(stm); 
      stm.Close(); 
관련 문제