Web Service 三种身份验证方式

Web Service 验证方式<一>SoapHeader验证

服务端代码:

注意第7行,需要加[SoapHeader("authHeader")] 特性。

1 public class GetDataList : System.Web.Services.WebService
2
{
3 public
AuthHeader authHeader;
4

5
6
[WebMethod]
7 [SoapHeader("authHeader"
)]
8 public string
GetBookList()
9
{
10 string xmlStr = string
.Empty;
11 try

12 {
13

14 if (authHeader == null)
15
{
16 XmlDocument xmlDoc = new
XmlDocument();
17
XmlDeclaration xmlDec;
18 xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null
);
19 XmlElement xmlEle = xmlDoc.CreateElement(null, "soapheader", null
);
20 XmlAttribute xmlAtt = xmlDoc.CreateAttribute("allowView"
);
21 xmlAtt.InnerText = "true"
;
22 XmlElement xmlChild = xmlDoc.CreateElement(null, "status", null
);
23
xmlChild.Attributes.Append(xmlAtt);
24 xmlChild.InnerText = "AuthHeader对象不存在"
;
25
xmlEle.AppendChild(xmlChild);
26
xmlDoc.AppendChild(xmlEle);
27

28 xmlStr = xmlDoc.InnerXml;
29

30 return xmlStr;
31
}
32 if (!
authHeader.Verify())
33
{
34 XmlDocument xmlDoc = new
XmlDocument();
35
XmlDeclaration xmlDec;
36 xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null
);
37 XmlElement xmlEle = xmlDoc.CreateElement(null, "soapheader", null
);
38 XmlAttribute xmlAtt = xmlDoc.CreateAttribute("allowView"
);
39 xmlAtt.InnerText = "true"
;
40 XmlElement xmlChild = xmlDoc.CreateElement(null, "status", null
);
41
xmlChild.Attributes.Append(xmlAtt);
42 xmlChild.InnerText = "验证失败"
;
43
xmlEle.AppendChild(xmlChild);
44
xmlDoc.AppendChild(xmlEle);
45

46 xmlStr = xmlDoc.InnerXml;
47

48 return xmlStr;
49
}
50

51 DataSet ds = new DataSet();
52 DataTable tb = new DataTable("BookList"
);
53 DataColumn dc0 = new DataColumn("Code", Type.GetType("System.String"
));
54 DataColumn dc1 = new DataColumn("BookName", Type.GetType("System.String"
));
55 DataColumn dc2 = new DataColumn("BookPrice", Type.GetType("System.Single"
));
56 DataColumn dc3 = new DataColumn("UpdateTime", Type.GetType("System.DateTime"
));
57

58
59 tb.Columns.Add(dc0);
60
tb.Columns.Add(dc1);
61
tb.Columns.Add(dc2);
62
tb.Columns.Add(dc3);
63

64 DataRow dr = tb.NewRow();
65 dr[0] = "c0001"
;
66 dr[1] = "世界地理"
;
67 dr[2] = "9.98"
;
68 dr[3] =
DateTime.Now;
69
tb.Rows.Add(dr);
70

71 dr = tb.NewRow();
72 dr[0] = "c0002"
;
73 dr[1] = "世界历史"
;
74 dr[2] = "9.95"
;
75 dr[3] =
DateTime.Now;
76
tb.Rows.Add(dr);
77

78 ds.Tables.Add(tb);
79 xmlStr =
ds.GetXml();
80
}
81 catch
(Exception ex)
82
{
83 xmlStr=
ex.ToString();
84
}
85

86 return xmlStr;
87
}
88
}
89 }

 

 

代码
public class AuthHeader:SoapHeader
{
public string UserCode
{
get;
set;
}

public string UserPwd
{
get;
set;
}

public bool Verify()
{
if (this.UserCode.Equals("yuli") && this.UserPwd.Equals("123456"))
return true;
else
return false;
}
}

 

 

客户端调用代码:

OA_WebService 添加web 引用时声明的命名空间。

1 protected void btnRun_Click(object sender, EventArgs e)
2
{
3 OA_WebService.GetDataList oaWebSerive = new
OA_WebService.GetDataList();
4

5 OA_WebService.AuthHeader authHeader = new OA_WebService.AuthHeader();
6

7 authHeader.UserCode = "alpha";
8 authHeader.UserPwd = "123456"
;
9

10 oaWebSerive.AuthHeaderValue = authHeader;
11

12 string str = oaWebSerive.GetBookList();
13

14 Response.Write(str);
15 }

 


Web Service 验证方式<二>IIS验证(windows集成验证)  

需要设置IIS“不允许匿名”访问并且需要选择“集成windows验证”,该验证方式服务端方法只有声明为[WebMethod]方法,不需要其他验证代码。

客户端调用代码,注意第5行,需要实例化webservice代理类的Credentials属性。

 

1 protected void Button1_Click(object sender, EventArgs e)
2 {
3 //将web服务程序设为集成windows身份验证,需要将匿名访问关闭
4   OA_WebService.GetDataListByIIS webService = new OA_WebService.GetDataListByIIS();
5 webService.Credentials = new System.Net.NetworkCredential("alpha", "123456");
6 string str = webService.GetBookList();
7 Response.Write(str);
8 }


Web Service 验证方式<三>Session验证

服务端代码,需要设置[WebMethod(EnableSession=true)] 允许Session。

1 public class GetDataListBySeesion : System.Web.Services.WebService
2
{
3 [WebMethod(EnableSession=true
)]
4 public bool Verify(string userName,string
pwd)
5
{
6 if (userName.Equals("alpha") && pwd.Equals("123456"
))
7
{
8 Session["LoginStatus"] = true
;
9
}
10 else

11 {
12 Session["LoginStatus"] = false
;
13
}
14

15 return (bool)Session["LoginStatus"];
16
}
17

18 [WebMethod(EnableSession=true)]
19 public string
GetBookList()
20
{
21 string xmlStr = string
.Empty;
22 try

23 {
24

25 if (Session["LoginStatus"] == null || Session["LoginStatus"].Equals(false))
26
{
27 XmlDocument xmlDoc = new
XmlDocument();
28
XmlDeclaration xmlDec;
29 xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null
);
30 XmlElement xmlEle = xmlDoc.CreateElement(null, "soapheader", null
);
31 XmlAttribute xmlAtt = xmlDoc.CreateAttribute("allowView"
);
32 xmlAtt.InnerText = "true"
;
33 XmlElement xmlChild = xmlDoc.CreateElement(null, "status", null
);
34
xmlChild.Attributes.Append(xmlAtt);
35 xmlChild.InnerText = "验证失败"
;
36
xmlEle.AppendChild(xmlChild);
37
xmlDoc.AppendChild(xmlEle);
38 xmlStr =
xmlDoc.InnerXml;
39 return
xmlStr;
40
}
41

42 DataSet ds = new DataSet();
43 DataTable tb = new DataTable("BookList"
);
44 DataColumn dc0 = new DataColumn("Code", Type.GetType("System.String"
));
45 DataColumn dc1 = new DataColumn("BookName", Type.GetType("System.String"
));
46 DataColumn dc2 = new DataColumn("BookPrice", Type.GetType("System.Single"
));
47 DataColumn dc3 = new DataColumn("UpdateTime", Type.GetType("System.DateTime"
));
48

49
50 tb.Columns.Add(dc0);
51
tb.Columns.Add(dc1);
52
tb.Columns.Add(dc2);
53
tb.Columns.Add(dc3);
54

55 DataRow dr = tb.NewRow();
56 dr[0] = "c0001"
;
57 dr[1] = "世界地理"
;
58 dr[2] = "9.98"
;
59 dr[3] =
DateTime.Now;
60
tb.Rows.Add(dr);
61

62 dr = tb.NewRow();
63 dr[0] = "c0002"
;
64 dr[1] = "世界历史"
;
65 dr[2] = "9.95"
;
66 dr[3] =
DateTime.Now;
67
tb.Rows.Add(dr);
68

69 ds.Tables.Add(tb);
70 xmlStr =
ds.GetXml();
71
}
72 catch
(Exception ex)
73
{
74 xmlStr =
ex.ToString();
75
}
76

77 return xmlStr;
78
}
79 }

客户端代码 ,注意第5行,需要实例化代理类的CookieContainer属性。否则会验证失败。 

1 protected void btnGoto_Click(object sender, EventArgs e)
2 {
3 OA_WebService.GetDataListBySeesion book = new OA_WebService.GetDataListBySeesion();
4
5 book.CookieContainer = new System.Net.CookieContainer();
6
7 if (book.Verify("alpha", "123456"))
8 {
9 string str = book.GetBookList();
10
11 Response.Write(str);
12 }
13 }

 

posted @ 2010-07-15 14:42  希腊字符  阅读(1328)  评论(0编辑  收藏  举报