Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebService标头身份验证_C#_.net_Web Services_Authentication_Soapheader - Fatal编程技术网

C# WebService标头身份验证

C# WebService标头身份验证,c#,.net,web-services,authentication,soapheader,C#,.net,Web Services,Authentication,Soapheader,正是现在,我获得了webservice身份验证,但我在WebMethod中调用了一个方法,如下所示: [WebMethod] [SoapHeader("LoginSoapHeader")] public int findNumberByCPF(string cpf) { try { LoginAuthentication(); var retRamal = DadosS

正是现在,我获得了webservice身份验证,但我在WebMethod中调用了一个方法,如下所示:

[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


提前感谢。

要求web服务客户端在访问web方法时必须提供用户名和密码

我们将使用自定义soap头而不是http头来实现这一点

NET framework允许您通过从SoapHeader类派生来创建自定义SOAP头,因此我们希望添加用户名和密码

using System.Web.Services.Protocols;

public class AuthHeader : SoapHeader
{
 public string Username;
 public string Password;
}
为了强制使用新的SOAP头,我们必须向该方法添加以下属性

[SoapHeader ("Authentication", Required=true)]
在.cs中包含类名

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;
 }            
}
我们在soap请求中使用soap:Header元素进行身份验证,不要误解随请求发送的HTTP头。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>

一串
一串

现在真的需要使用旧的SOAP协议吗?有许多仅使用HTTP的替代方案。例如,Web API,它是纤细、灵活的,并且允许轻松实现不同的身份验证场景(请参阅)。这不是真的需要,但我想学习如何使用SOAP来实现这一点。在我看来,SOAP已经过时了。所以我认为学习SOAP与Autosist的工作很接近。您是否遵循纯粹的教育目标,或者您在尝试使用SOAP实现时遇到了一些问题?我只是在遵循过程,然后我必须使用SOAP来执行此操作。如果您使用的是成员身份,请使用以下行来验证用户:Membership.ValidateUser(Authentication.Username,Authentication.Password)@如果你认为我的答案是有用的…你可以投票给我的答案,这样答案搜索者就会知道答案的可用性。谢谢