2013-08-06 2 views
5

정확히 지금, 나는 내 웹 서비스 인증을 가지고,하지만 난 이런 식으로,의 WebMethod 내부에이 호출하는 방법을 수행했습니다WebService를 헤더 인증

[WebMethod] 
[SoapHeader("LoginSoapHeader")] 
public int findNumberByCPF(string cpf) 
     { 
      try 
      { 
       LoginAuthentication(); 
       var retRamal = DadosSmp_Manager.RetornaRamalPorCPF(cpf); 
       var searchContent= String.Format("CPF[{0}]", cpf); 
       DadosSmp_Manager.insertCallHistory(retRamal, searchContent); 

       return retRamal.Ramal; 
      } 
      catch (Exception ex) 
      { 
       Log.InsertQueueLog(Log.LogType.Error, ex); 
       throw getException(ex.TargetSite.Name, cpf); 
      } 
     } 

내가 (이하 "LoginAuthentication를 호출하지 않고이의 WebMethod를 인증하기 위해 지금 싶어) "메서드를 사용하여 코드 안의 SOAP 헤더 - SoapHeader ("LoginSoapHeader ") 만 사용합니다.

그런 다음 제 질문은 헤더 만 사용하여 WebMethod를 인증하는 방법입니다.

미리 감사드립니다.

+0

이전 SOAP 프로토콜을 사용하려면 * 정말 필요한가요? HTTP 만 사용하는 많은 대안이 있습니다. 예를 들어 슬림하고 유연하며 다른 인증 시나리오를 쉽게 구현할 수있는 웹 API ([여기] (http://www.asp.net/web-api/overview/security) 참조). – Oleg

+0

정말 필요하지 않지만 SOAP을 사용하여이를 수행하는 방법을 배우고 싶습니다. – guisantogui

+0

SOAP은 죽었습니다. 그래서 SOAP의 학습은 오토리스트의 일과 가깝다고 생각합니다. 순수 교육 목표를 따르고 있습니까? 아니면 SOAP을 사용하여 구현하려고하는 문제가 있습니까? – Oleg

답변

11

요구 사항은 웹 서비스 클라이언트가 웹 메소드에 액세스하는 동안 사용자 이름과 비밀번호를 제공해야한다는 것입니다.

우리는이 사용하여 사용자 정의 SOAP 헤더가 아닌 HTTP 헤더

닷넷 프레임 워크, 당신은의 SOAPHeader 클래스에서 파생하여 사용자 지정 SOAP 헤더를 만들 수 있습니다 달성하기 위하여려고하고 그래서 우리는 사용자 이름과 암호

를 추가하고 싶었
using System.Web.Services.Protocols; 

public class AuthHeader : SoapHeader 
{ 
public string Username; 
public string Password; 
} 

[SoapHeader ("Authentication", Required=true)] 

이 일 포함하는 방법으로 다음과 같은 속성을 추가 우리가 우리의 새로운 SOAP 헤더의 사용을 강제로 .cs에서 E 클래스 이름

public AuthHeader Authentication; 


[SoapHeader ("Authentication", Required=true)] 
[WebMethod (Description="WebMethod authentication testing")] 
public string SensitiveData() 
{ 

//Do our authentication 
//this can be via a database or whatever 
if(Authentication.Username == "userName" && 
      Authentication.Password == "pwd") 
{ 
    //Do your thing 
    return ""; 

} 
else{ 
    //if authentication fails 
    return null; 
}    
} 

우리가 비누를 사용하여 인증 : 요청과 함께 전송 된 HTTP 헤더를 오해하지 마십시오 SOAP 요청에서 헤더 요소를. SOAP 요청은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Header> 
    <AUTHHEADER xmlns="http://tempuri.org/"> 
    <USERNAME>string</USERNAME> 
    <PASSWORD>string</PASSWORD> 
    </AUTHHEADER> 
</soap:Header> 
    <soap:Body> 
    <SENSITIVEDATA xmlns="http://tempuri.org/" /> 
    </soap:Body> 
</soap:Envelope> 
+2

회원을 사용하는 경우 다음 줄을 사용하여 사용자의 유효성을 검사하십시오. Membership.ValidateUser (Authentication.Username, Authentication.Password) –

+0

@AtronSeige - 네 말이 맞습니다! –

+0

@AtronSeige - 내 대답이 유용하다고 생각하는 경우 ... 내 답변을 상향 조정하여 답변을 찾는 사람이 대답의 유용성을 알 수 있도록 할 수 있습니다. 감사합니다. –