当前位置: 代码迷 >> Web Service >> 怎么使用soapheader进行安全认证
  详细解决方案

怎么使用soapheader进行安全认证

热度:512   发布时间:2016-05-02 03:11:59
如何使用soapheader进行安全认证
服务器端已经建立一下文件,请问客户端如何进行认证 并调用 HelloWorld


Imports System.Web
Imports System.Data
Imports System.Web.Services
Imports System.Web.Services.Protocols

  Public Class Authenticator : Inherits SoapHeader
  Public UserName As String
  Public UserPassWord As String
  End Class

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
  Inherits System.Web.Services.WebService
  Dim db As New XMYH.Data
  Public sHeader As authenticator
  Dim cmdText As String

  Function Authenticate(ByVal strUser As String, ByVal strPass As String) As Boolean
  If strUser = "user" And strPass = "pass" Then
  Return True
  Else
  Return False
  End If
  End Function


  <WebMethod(), SoapHeader("sHeader")> _
  Public Function HelloWorld() As String
  If sHeader Is Nothing Then
  Throw New ArgumentException
  End If

  Return "Hello World"
  End Function

End Class


------解决方案--------------------
从本版其他帖子 复制了解答如下:

你通过添加 Service Reference 生成本地代理。

然后按照类似如下的代码,添加用户名和密码
C# code
using (var client = new ServiceReference1.Service1Client())
{
client.Headers.username = "";
client.Headers.password = "";
...
}
------解决方案--------------------
创建一个MyHeader类 
 /// <summary>
/// 安全加密类(在文件头上进行加密)
/// </summary>
public class MySoapHeader:SoapHeader
{
private string _UserName=string.Empty;
private string _PassWord=string.Empty;
private Hashtable htUserArray=new Hashtable();
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
set { _UserName = value; }
get { return _UserName; }
}
/// <summary>
/// 密码
/// </summary>
public string PassWord
{
set { _PassWord = value; }
get { return _PassWord; }
}
public MySoapHeader()
{
htUserArray.Add("name", "pwd");
}


/// <summary>
/// 验证是否有权限访问webservice
/// </summary>
/// <returns>存在与否</returns>
public bool IsValid()
{
if(htUserArray.ContainsKey(UserName)&&htUserArray.ContainsValue(PassWord))
{
return true;
}
else
{
return false;
}
}

}
在webservice页面这么实例化就行:
 public MySoapHeader MyHeader = new MySoapHeader();


要调用接口的客户下边这样调用就行了
private string GetEdiData()
{
WebGetTransportData.MySoapHeader Header = new WMS.Web.WebGetTransportData.MySoapHeader();
WebGetTransportData.TmsService server = new WMS.Web.WebGetTransportData.TmsService();
Header.UserName = "name";//用户名
Header.PassWord = "pwd";//密码
server.MySoapHeaderValue = Header;
string Onumber = OrderNumber.Text.Trim();//条件
//调用web方法,得到接口返回值
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!