C# web服务中使用SOAP头的身份验证

C# web服务中使用SOAP头的身份验证,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我试图使用SOAP为一个web服务实现身份验证,但似乎在header中发送的元素总是null 这是我的[WebMethod]: [WebService(Namespace = "TestWService")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class WebService : System.Web.Ser

我试图使用
SOAP
为一个web服务实现身份验证,但似乎在header中发送的元素总是
null

这是我的
[WebMethod]

[WebService(Namespace = "TestWService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService : System.Web.Services.WebService
{
public AuthenticateUser User;
[WebMethod]
[SoapHeader("User")]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public void AllCountriesInfo()
{
 Context.Response.Write(JsonConvert.SerializeObject(User));
 if (User != null)
 {
  if (User.IsValid())
  { 
   Context.Response.Write(JsonConvert.SerializeObject("User is authenticated"));
  }
 }
Context.Response.Write(JsonConvert.SerializeObject("User Data is missing"));
}
}
AuthenticateUser
类别:

public class AuthenticateUser : System.Web.Services.Protocols.SoapHeader
{
 public string UserName { get; set; }
 public string Password { get; set; }

 public bool IsValid()
 {
  AccountMembershipService MembershipService = new AccountMembershipService();
  return MembershipService.ValidateUser(UserName, Password);
 }
}
以及
请求

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthenticateUser xmlns="TestWService">
      <UserName>testadmin</UserName>
      <Password>pass</Password>
    </AuthenticateUser>
  </soap:Header>
  <soap:Body>
    <AllCountriesInfo xmlns="TestWService" />
  </soap:Body>
</soap:Envelope>

测试管理员
通过

有人能指出我所犯错误的方向吗?

@bommelding如果有,我没有包括在问题中。但是现在它已经存在了。您需要在
AllCountriesInfo
方法上指定一个返回类型。它当前返回
void