自定义SOAP标题可以限制调用服务的用户范围
1
2
using System.Web;
3
using System.Web.Services;
4
using System.Web.Services.Protocols;
5
6
[WebService(Namespace = "http://livebaby.cn")]
7
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
8
public class Service : System.Web.Services.WebService
9

{
10
public SecurityHeader currentUser;
11
public Service()
12
{
13
14
//如果使用设计的组件,请取消注释以下行
15
//InitializeComponent();
16
}
17
[WebMethod, SoapHeader("currentUser")]
18
public string GetResult(string queryString)
19
{
20
if(ValidateUser(currentUser.UserName,currentUser.UserPass))
21
{
22
return "你发送的字符串是:"+queryString;
23
}
24
else
25
return "对不起:" + currentUser.UserName+",您不是合法的用户!";
26
}
27
//检验SOAP HEADER
28
private bool ValidateUser(string user, string pass)
29
{
30
if (user.Equals("user") && pass.Equals("user"))
31
return true;
32
else
33
return false;
34
}
35
}
36
//自定义Soap Header Class
37
public class SecurityHeader : System.Web.Services.Protocols.SoapHeader
38

{
39
public string UserName;
40
public string UserPass;
41
}
using System;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
下面是客户端的调用
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace SoapHeader
10

{
11
public partial class Form1 : Form
12
{
13
public Form1()
14
{
15
InitializeComponent();
16
}
17
18
private void button_Invoke_Click(object sender, EventArgs e)
19
{
20
SoapHeader.localhost.SecurityHeader header = new SoapHeader.localhost.SecurityHeader();
21
header.UserName = textBox_User.Text;
22
header.UserPass = textBox_Pass.Text;
23
SoapHeader.localhost.Service service = new SoapHeader.localhost.Service();
24
service.SecurityHeaderValue = header;
25
this.textBox_Output.Text+=service.GetResult(this.textBox_Input.Text)+Environment.NewLine;
26
}
27
}
28
}
29
保留地址 http://www.qqread.com/dotnet/j311175.html进入讨论组讨论。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- 用vb.net实现闹钟提醒程序 (9次浏览)
- C#变得越来越臃肿是不可避免的? (7次浏览)
- vb.net GDI+入门——画笔、画刷和颜色 (3次浏览)
- C# 3.0新特性之扩展方法 (1次浏览)
- ASP.NET 3.5 Extensions带来什么 (1次浏览)
- WPF的Attached属性 (0次浏览)
- WPF,将颠覆的设计世界? (0次浏览)
- Windows是否已经变成了一个怪物? (0次浏览)
- Windows2008用RODC保证分支机构安全 (0次浏览)
- Windows2008的NLB配置攻略 (0次浏览)



