web service身份验证
服务端:
1 public class Service1 : System.Web.Services.WebService
2 {
3 public myHeader myhead = new myHeader();
4 [WebMethod(Description="DataSet ,读取myTest数据库中的tb_test表")]
5 [SoapHeader("myhead")]
6 public DataSet getDataSet()
7 {
8 if (myhead.UserName=="ywn"&&myhead.UserPwd=="ywn")
9 {
10 SqlConnection sqlCon = new SqlConnection("server=localhost;user id=sa;password=sa;database=MyTestDB");
11 sqlCon.Open();
12 SqlDataAdapter da = new SqlDataAdapter("select * from employee", sqlCon);
13 DataSet ds = new DataSet();
14 da.Fill(ds);
15 ds.Dispose();
16 return ds;
17 }
18 else
19 {
20 return null;
21 }
22 }
23 }
24 public class myHeader : System.Web.Services.Protocols.SoapHeader
25 {
26 string username;
27 string userpwd;
28 public string UserName
29 {
30 get {return this.username; }
31 set{this.username=value;}
32 }
33 public string UserPwd
34 {
35 get;
36 set;
37 }
38 }
39
客户端,引用web服务
1 protected void Button1_Click1(object sender, EventArgs e)
2 {
3 localhost.myHeader header = new WebClient.localhost.myHeader();
4 header.UserName = this.TextBox1.Text;
5 header.UserPwd = this.TextBox2.Text;
6 localhost.Service1 ser = new WebClient.localhost.Service1();
7 ser.myHeaderValue = header;
8 if (ser.getDataSet() != null)
9 {
10 this.GridView1.DataSource = ser.getDataSet();
11 this.GridView1.DataBind();
12 }
13 else
14 {
15 this.GridView1.EmptyDataText = "你输入的不对,没有权限应用该服务";
16 this.GridView1.DataBind();
17 }
18 }
19
posted on 2009-04-02 03:45 YWN's blog 阅读(211) 评论(0) 收藏 举报
浙公网安备 33010602011771号