首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >soapheader身份验证

soapheader身份验证
EN

Stack Overflow用户
提问于 2012-08-28 19:42:40
回答 3查看 10.4K关注 0票数 1

我不能让我的客户识别AuthHeaderValue,这是visual studio添加到proxy类中的,我看过很多例子,但找不到如何解决这个问题的方法。

Soap类

代码语言:javascript
复制
     public class AuthHeader : SoapHeader
     {
        public string Username;
        public string Password;

     }

web服务

代码语言:javascript
复制
     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";
        }            
    }

客户端

代码语言:javascript
复制
   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);

    }
EN

回答 3

Stack Overflow用户

发布于 2013-04-24 09:22:22

您必须将身份验证参数值作为标头的TRUE进行传递。查看完整的解决方案here

票数 0
EN

Stack Overflow用户

发布于 2013-08-21 18:55:49

代码语言:javascript
复制
WebService service = new WebService();
       service.Authentication.Username = "a";
       service.Authentication.Password = "a";
       string str = service .CheckAuthn();

应用该代码

票数 0
EN

Stack Overflow用户

发布于 2016-08-12 12:26:41

尝尝这个

代码语言:javascript
复制
public ServiceAuthHeader Credentials;
public class ServiceAuthHeader : SoapHeader
{
    public string Username;
    public string Password;
}

要更改安全方法,如下所示

代码语言:javascript
复制
 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;
 }

将带有凭据的方法保存在某个公共类中或其他位置

代码语言:javascript
复制
 public static void AuthValidatoin(WebserviceObject callwebservice)
 {
       ServiceAuthHeader serviceAuthHeaderValue = new LocalERPWebService.ServiceAuthHeader();
       serviceAuthHeaderValue.Username = "test";
       serviceAuthHeaderValue.Password = "test";
       callwebservice.ServiceAuthHeaderValue = serviceAuthHeaderValue;
 } 

调用web服务时,请使用上面的方法对服务进行身份验证,如下所示

代码语言:javascript
复制
 WebserviceObject CallWebService = new WebserviceObject();
 common.AuthValidatoin(CallWebService);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12158545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档