1

我无法让我的客户协调视觉工作室添加到代理类的 AuthHeaderValue 我已经看到了很多示例,但找不到任何关于如何解决此问题的信息。

肥皂类

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

     }

网络服务

     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);

    }
4

3 回答 3

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

应用该代码

于 2013-08-21T10:55:48.927 回答
0

您必须像TRUE标头一样传递身份验证参数值。在此处查看完整的解决方案

于 2013-04-24T01:22:22.013 回答
0

尝试这个

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

安全方法更改如下

 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 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);
于 2016-08-12T04:26:41.297 回答