我咨询这两个网站只是为了知道如何使用soap header
https://www.codeproject.com/articles/4398/authentication-for-web-services-using-soap-headers
https://www.codeproject.com/Articles/27365/Authenticate-NET-Web-Service-with-Custom-SOAP-Head
我也以这种方式开发我的网络服务。看到代码
namespace CustomAuthWithHTTPModule
{
/// <summary>
/// Summary description for SecureWebService
/// </summary>
public class Authentication : SoapHeader
{
public string User;
public string Password;
}
public class SecureWebService : WebService
{
public Authentication authentication;
[WebMethod]
[SoapHeader("authentication", Required = true)]
public string ValidUser()
{
if (User.IsInRole("Customer"))
return "User is in role customer";
if (User.Identity.IsAuthenticated)
return "User is a valid user";
return "not authenticated";
}
}
}
在我的控制台应用程序中,我只需添加Web引用即可创建上述Web服务的代理。问题是AuthHeaderValue或UserCredentialsValue这两个属性都没有在客户端公开,我可以通过它发送用户凭据。看我的客户代码
ServiceReference1.SecureWebServiceSoapClient xx = new ServiceReference1.SecureWebServiceSoapClient();
ServiceReference1.Authentication a = new ServiceReference1.Authentication();
a.User = "Hello";
a.Password = "Hello";
xx.AuthHeaderValue or xx.UserCredentialsValue // not being expose
指导我犯错的地方。我正在使用VS2013 IDE。感谢