2012-08-28 26 views
1

我不能讓我的客戶reconise AuthHeaderValue其視覺工作室添加到我見過很多例子,但能找到如何解決這個沒什麼代理類 。SOAPHEADER認證

皁類

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

    } 

Web服務

 public class Service1 : System.Web.Services.WebService 
    { 
     public AuthHeader Authentication; ** where does visual studio append value to proxy 

    [SoapHeader("Authentication", Required = true)] 
    [WebMethod] 
    public string security() 
    { 
     if (Authentication.Username == "test" && 
      Authentication.Password == "test") 
     { 
      return "authenticated"; 
     } 
     else 
     { 
      return "get lost"; 
     }    
    } 

客戶

static void Main(string[] args) 
    { 
     ServiceReference1.AuthHeader auth = new ServiceReference1.AuthHeader(); 
     auth.Username = "test"; 
     auth.Password = "test"; 

     ServiceReference1.Service1SoapClient ser = new ServiceReference1.Service1SoapClient(); 
     ser.AuthHeaderValue = auth; ** does not reconise authheadervalue 
     String message = ser.security(); 
     Console.WriteLine(message); 

    } 
+1

固定它來驗證服務。添加服務引用而不是Web引用。 DUU –

回答

0

您必須驗證參數值傳遞作爲TRUE的頭。 檢查完整的解決方案here

0
WebService service = new WebService(); 
     service.Authentication.Username = "a"; 
     service.Authentication.Password = "a"; 
     string str = service .CheckAuthn(); 

應用代碼

0

嘗試像下面

public static string Validate(ServiceAuthHeader soapHeader) 
{ 
     string error_msg = "Pass"; 
     if (soapHeader == null) 
     { 
      error_msg = "No soap header was specified."; 
     } 
     else if (soapHeader.Username == null || soapHeader.Username == "") 
     { 
      error_msg = "Username was not supplied for authentication in SoapHeader."; 
     } 
     else if (soapHeader.Password == null || soapHeader.Password == "") 
     { 
      error_msg = "Password was not supplied for authentication in SoapHeader."; 
     } 

     else if (soapHeader.Username != "test" || soapHeader.Password != "test") 
     { 
      error_msg = "Please pass the proper username and password for this service."; 
     } 

     return error_msg; 
} 

保持方法,在您的一些證書此

public ServiceAuthHeader Credentials; 
public class ServiceAuthHeader : SoapHeader 
{ 
    public string Username; 
    public string Password; 
} 

安全方法改變普通班或其他地方

public static void AuthValidatoin(WebserviceObject callwebservice) 
{ 
     ServiceAuthHeader serviceAuthHeaderValue = new LocalERPWebService.ServiceAuthHeader(); 
     serviceAuthHeaderValue.Username = "test"; 
     serviceAuthHeaderValue.Password = "test"; 
     callwebservice.ServiceAuthHeaderValue = serviceAuthHeaderValue; 
} 
當你調用Web服務

用上面的方法類似下面

WebserviceObject CallWebService = new WebserviceObject(); 
common.AuthValidatoin(CallWebService);