https://www.cnblogs.com/Hawk-Hong/p/4293651.html

在项目开发,我们经常会使用WebService,但在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService?从哪里引用我的WebService?对于第一个问题,就涉及到了WebService是安全问题,因为我们提供的WebService不是允许所有人能引用 的,可能只允许本公司或者是通过授权的人才能使用的。那怎么防止非法用户访问呢?很容易想到通过一组用户名与密码来防止非法用户的调用 。
       在System.Net中提供了一个NetworkCredential,通过它我们可以在网络中提供一个凭证,只有获得该凭证的用户才能访问相应的服务的权限。在这里我们也使用NetworkCredential。在NetworkCredential中,我们通过提供WebService发布所在的服务器名称,以及登录服务器并调用该WebService的用户名及密码(在IIS中配置)。
在调用WebService时设置其Credential属性,把上面得到的Credential凭证赋值给它,这样只有使用提供的用户名及密码才能调用WebService服务了而其他用户则无法访问,这样就能能满足防止WebService被别人调用了。
       至于主机名,用户名及密码,对于B/S可以通过webconfig来配置,对于C/S可以使用应用程序配置文件。这样就能灵活地配置了。
如下以C/S为例来说明,首先我们提供一个服务器网络凭证,然后通过WebRequest来验证连接是否成功。当然了,为了保存用户名与密码等的安全,可以对其进行加密等手段来保证其安全。

以下是主要源代码:

1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
1        /**//// <summary>
 2        /// 服务器网络凭证
 3        /// </summary>
 4        /// <returns></returns>
 5        public static NetworkCredential MyCred()
 6        {
 7            string loginUser = Properties.Settings.Default.UserName;//用户名
 8            string loginPSW = Properties.Settings.Default.UserPSW;//密码
 9            string loginHost = Properties.Settings.Default.HostName;//主机名,可以是IP地址,也可以服务器名称
10            NetworkCredential myCred = new NetworkCredential(loginUser,loginPSW, loginHost);
11            //NetworkCredential myCred = new NetworkCredential("username", "123456", "yourip");//"username", "123456", "yourservername"
12            return myCred;
13        }
14        /**//// <summary>
15        /// 验证是否成功连接到服务器,若连接成功,则返回TRUE
16        /// </summary>
17        /// <param name="url">服务器WebService URL</param>
18        /// <returns></returns>
19        public static bool Credential(string url)
20        {
21           //定义局部变量
22           string url = G_Url;//2009-02-25   服务器验证只验证到机器
23
24            try
25            {
26                if (myWebResponse == null)
27                {
28                    WebRequest myWebRequest = WebRequest.Create(url);//根据URL创建一个连接请求
29                    myWebRequest.Credentials = MyCred();//获取验证的凭证,这是最重要的一句
30                    myWebRequest.Timeout = 20000;//单位为毫秒
31
32                    myWebResponse = myWebRequest.GetResponse();//返回连接成功时的信息
33                }
34            }
35            catch (WebException wex)//无法连接到服务器,可能是因为服务器错误或用户名与密码错误
36            {
37                if (myWebResponse != null)//毁销
38                {
39                    myWebResponse.Close();
40                    myWebResponse = null;
41                }
42
43                return false;
44            }
45            catch (Exception ex)
46            {
47                if (myWebResponse != null)
48                {
49                    myWebResponse.Close();
50                    myWebResponse = null;
51                }
52
53                return false;
54
55            }
56            finally
57            {
58            }
59
60            return true;
61        }
62
63       private static WS_Webasic.WS_Webasic webasic =null;//实现华WS_Webasic.WS_Webasic
64
65        /**//// <summary>
66        /// WS_Webasic初始化
67        /// </summary>
68        public static WS_Webasic.WS_Webasic WS_Webasic
69        {
70            get
71            {
72                if (webasic == null)//若webasic 为空,则重新实例化,这样可以减少验证的时间,提高效率
73                {
74                    //webasic = new ZEDI.WS_Webasic.WS_Webasic();
75                    //wsBool = Credential(webasic.Url);//URL改为服务器地址 2009-02-25  chhuic@163.com
76                    wsBool = Credential(G_Url);
77                    if (wsBool == true)  //服务器连接验证通过
78                    {
79                        webasic = new WS_Webasic.WS_Webasic();//实例化
80                        webasic.Credentials = MyCred();//得到服务器连接凭证,这样该WebService可以放心的连接了
81                    }
82                }
83                return webasic;
84            }
85        }

注:
(1)必须引用 System.Net;
(2)对WebService发访问,在IIS里取消匿名访问权限,若允许匿名访问,就没有必须提供验证凭证了。IIS里怎么取消匿名访问 权限请参照IIS相关文章,这里不在累赘。
验证是有时速度会比较慢,主要是因为myWebResponse = myWebRequest.GetResponse();时速度比较慢

Credential的更多相关文章

  1. Cannot set a credential for principal 'sa'. (Microsoft SQL Server,错误: 15535)

    在SQL SERVER 2008上上禁用sa登录时,遇到下面错误:"Cannot set a credential for principal 'sa'. (Microsoft SQL Se ...

  2. git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin

    git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origi ...

  3. Dynamics Webservice Call with Credential

    Dynamics Webservice call with credential /// <summary> ///WebServiceHelper 的摘要说明 /// </summ ...

  4. Azure 自动化:使用PowerShell Credential连接到Azure

    最近在中国版windows azure中新上线的自动化功能, 使用自动化,您可以导入自己的PowerShell脚本,然后设置一个运行计划使得脚本能按计划运行. 在本文中,我们来学习如何使用PowerS ...

  5. SQL Server 2008 - Cannot set a credential for principal 'sa'.

    SQL Server 2008 - Cannot set a credential for principal 'sa'. 很久没有用到SQL Server了,今天有幸在帮同事解决一个SQL Serv ...

  6. asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  7. asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  8. 凭证(Credential)

    在SQL Server中,凭证(Credential)用于把Windows用户的身份验证信息(在Windows环境下,是Windows 用户名和密码)存储在SQL Server实例中,并把该身份验证信 ...

  9. The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the '##xp_cmdshell_proxy_account##' credential exists and contains valid information.

    In one of our recent migrations, we got the following error when the client tried to fire xp_cmdshel ...

  10. You earned your Program Management Professional (PgMP)® Credential

    You earned your Program Management Professional (PgMP)® Credential. pasting

随机推荐

  1. eclipse中类和方法添加作者日期说明

    1.类添加作者日期说明 依次点击window—>preferences—>Java—>Code Stype—>Code Templates-Comments-Types 2.方 ...

  2. 说说http协议中的编码和解码

    http://www.csdn1 2 3.com/html/itweb/20130730/29422_29378_29408.htm ****************************** 一. ...

  3. 常用的兼容IE和火狐FF等浏览器的js方法(js中ie和火狐的一些差别)

    介绍了网页上常用的IE/火狐兼容性该页的做法,并给出了代码,相当实用了.为了方便大家阅读代码,以下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefo ...

  4. 登陆时不同浏览器获取session存在的相关疑问?

    问题1:在同一个电脑上,登陆成功后,将登陆信息存放到session域中后,使用另一个浏览器访问时,能否获取这个session域中的值? request.getSession().setAttribut ...

  5. weblogic检查项

    日常维护中,weblogic检查的几个项: 1.JVM: 如最大堆内存.最小堆内存. 2.GC回收: 查看jvm空闲内存变化情况,每次GC的回收情况:控制台可以强制垃圾回收,看看回收内存是否太小,如果 ...

  6. 多进程监控自动关机工具升级远程关闭多台server——C# works with PowerShell

    之前给单位做过一个多进程监控的自动关机工具,详见那篇blog. 这次领导又加了需求,说要等进程监控结束后,不止需要关闭主控端server,还需要关闭其他servers.于是就用到了我上篇文章所介绍的知 ...

  7. am335x USB 驱动框架记录

    参考: http://processors.wiki.ti.com/index.php/AM335x_USB_Driver%27s_Guide http://processors.wiki.ti.co ...

  8. Liunx下NFS服务器的搭建与配置

    一.NFS简介 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NF ...

  9. 6款强大的 jQuery 网页布局创建及优化插件

    本文将为您介绍6款功能强大的jQuery插件,它们能够帮助您方便快捷地创建复杂的网络布局并进行优化. 1.UI.Layout 该插件可以创建任何你想要的UI形式:包括从简单的标题或侧边栏,到一个包含工 ...

  10. WCF系列

    转自:1) http://www.cnblogs.com/zhili/p/WCFSummary.html 2)  http://www.cnblogs.com/artech/archive/2009/ ...