C#調用Web Service時的身份驗證
在System.Net中提供了一個NetworkCredential,經過它咱們能夠在網絡中提供一個憑證,只有得到該憑證的用戶才能訪問相應的服務的權限。在這裏咱們也使用NetworkCredential。在NetworkCredential中,咱們經過提供WebService發佈所在的服務器名稱,以及登陸服務器並調用該WebService的用戶名及密碼(在IIS中配置)。
在調用WebService時設置其Credential屬性,把上面獲得的Credential憑證賦值給它,這樣只有使用提供的用戶名及密碼才能調用WebService服務了而其餘用戶則沒法訪問,這樣就能能知足防止WebService被別人調用了。
至於主機名,用戶名及密碼,對於B/S能夠經過webconfig來配置,對於C/S可使用應用程序配置文件。這樣就能靈活地配置了。
以下以C/S爲例來講明,首先咱們提供一個服務器網絡憑證,而後經過WebRequest來驗證鏈接是否成功。固然了,爲了保存用戶名與密碼等的安全,能夠對其進行加密等手段來保證其安全。
如下是主要源代碼:
![](http://static.javashuo.com/static/loading.gif)
1
![](http://static.javashuo.com/static/loading.gif)
2
![](http://static.javashuo.com/static/loading.gif)
3
![](http://static.javashuo.com/static/loading.gif)
4
![](http://static.javashuo.com/static/loading.gif)
5
![](http://static.javashuo.com/static/loading.gif)
6
![](http://static.javashuo.com/static/loading.gif)
7
![](http://static.javashuo.com/static/loading.gif)
8
![](http://static.javashuo.com/static/loading.gif)
9
![](http://static.javashuo.com/static/loading.gif)
10
![](http://static.javashuo.com/static/loading.gif)
11
![](http://static.javashuo.com/static/loading.gif)
12
![](http://static.javashuo.com/static/loading.gif)
13
![](http://static.javashuo.com/static/loading.gif)
14
![](http://static.javashuo.com/static/loading.gif)
15
![](http://static.javashuo.com/static/loading.gif)
16
![](http://static.javashuo.com/static/loading.gif)
17
![](http://static.javashuo.com/static/loading.gif)
18
![](http://static.javashuo.com/static/loading.gif)
19
![](http://static.javashuo.com/static/loading.gif)
20
![](http://static.javashuo.com/static/loading.gif)
21
![](http://static.javashuo.com/static/loading.gif)
22
![](http://static.javashuo.com/static/loading.gif)
23
![](http://static.javashuo.com/static/loading.gif)
24
![](http://static.javashuo.com/static/loading.gif)
25
![](http://static.javashuo.com/static/loading.gif)
26
![](http://static.javashuo.com/static/loading.gif)
27
![](http://static.javashuo.com/static/loading.gif)
28
![](http://static.javashuo.com/static/loading.gif)
29
![](http://static.javashuo.com/static/loading.gif)
30
![](http://static.javashuo.com/static/loading.gif)
31
![](http://static.javashuo.com/static/loading.gif)
32
![](http://static.javashuo.com/static/loading.gif)
33
![](http://static.javashuo.com/static/loading.gif)
34
![](http://static.javashuo.com/static/loading.gif)
35
![](http://static.javashuo.com/static/loading.gif)
36
![](http://static.javashuo.com/static/loading.gif)
37
![](http://static.javashuo.com/static/loading.gif)
38
![](http://static.javashuo.com/static/loading.gif)
39
![](http://static.javashuo.com/static/loading.gif)
40
![](http://static.javashuo.com/static/loading.gif)
41
![](http://static.javashuo.com/static/loading.gif)
42
![](http://static.javashuo.com/static/loading.gif)
43
![](http://static.javashuo.com/static/loading.gif)
44
![](http://static.javashuo.com/static/loading.gif)
45
![](http://static.javashuo.com/static/loading.gif)
46
![](http://static.javashuo.com/static/loading.gif)
47
![](http://static.javashuo.com/static/loading.gif)
48
![](http://static.javashuo.com/static/loading.gif)
49
![](http://static.javashuo.com/static/loading.gif)
50
![](http://static.javashuo.com/static/loading.gif)
51
![](http://static.javashuo.com/static/loading.gif)
52
![](http://static.javashuo.com/static/loading.gif)
53
![](http://static.javashuo.com/static/loading.gif)
54
![](http://static.javashuo.com/static/loading.gif)
55
![](http://static.javashuo.com/static/loading.gif)
56
![](http://static.javashuo.com/static/loading.gif)
57
![](http://static.javashuo.com/static/loading.gif)
58
![](http://static.javashuo.com/static/loading.gif)
59
![](http://static.javashuo.com/static/loading.gif)
60
![](http://static.javashuo.com/static/loading.gif)
61
![](http://static.javashuo.com/static/loading.gif)
62
![](http://static.javashuo.com/static/loading.gif)
63
![](http://static.javashuo.com/static/loading.gif)
64
![](http://static.javashuo.com/static/loading.gif)
65
![](http://static.javashuo.com/static/loading.gif)
66
![](http://static.javashuo.com/static/loading.gif)
67
![](http://static.javashuo.com/static/loading.gif)
68
![](http://static.javashuo.com/static/loading.gif)
69
![](http://static.javashuo.com/static/loading.gif)
70
![](http://static.javashuo.com/static/loading.gif)
71
![](http://static.javashuo.com/static/loading.gif)
72
![](http://static.javashuo.com/static/loading.gif)
73
![](http://static.javashuo.com/static/loading.gif)
74
![](http://static.javashuo.com/static/loading.gif)
75
![](http://static.javashuo.com/static/loading.gif)
76
![](http://static.javashuo.com/static/loading.gif)
77
![](http://static.javashuo.com/static/loading.gif)
78
![](http://static.javashuo.com/static/loading.gif)
79
![](http://static.javashuo.com/static/loading.gif)
80
![](http://static.javashuo.com/static/loading.gif)
81
![](http://static.javashuo.com/static/loading.gif)
82
![](http://static.javashuo.com/static/loading.gif)
83
![](http://static.javashuo.com/static/loading.gif)
84
![](http://static.javashuo.com/static/loading.gif)
85
![](http://static.javashuo.com/static/loading.gif)
(1)必須引用 System.Net;
(2)對WebService發訪問,在IIS裏取消匿名訪問權限,若容許匿名訪問,就沒有必須提供驗證憑證了。IIS裏怎麼取消匿名訪問 權限請參照IIS相關文章,這裏不在累贅。
驗證是有時速度會比較慢,主要是由於myWebResponse = myWebRequest.GetResponse();