温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# WebService详解

发布时间:2020-08-11 14:32:50 来源:网络 阅读:522 作者:Aonaufly 栏目:编程语言

这里直接讲解WebService的实现过程及其中应该注意的点,有关其应用的环境等请度娘或者google。

首先新建一个WebService服务:如图:

C# WebService详解

我的WebService的结构如下图:

C# WebService详解

好了,这里主要讲解身份验证类以及asmx服务使用身份验证应该注意的问题:

身份验证类:(需要继承System.Web.Services.Protocols.SoapHeader)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
namespace WebEmpty
{
    public class Authentication_WS : SoapHeader
    {
       private string userID = string.Empty;
       private string userPW = string.Empty;
       public string UserId
       {
           get { return userID; }
           set { userID = value; }
       }
       public string UserPW
       {
           get { return userPW; }
           set { userPW = value; }
       }
       public Authentication_WS()
       { }
       public Authentication_WS(string name, string password)
       {
           userID = name;
           userPW = password;
       }
       private bool IsValid(string nUserId, string nPassWord, out string nMsg)
       {
           nMsg = "";
           try
           {
               if (nUserId == "admin" && nPassWord == "admin")
               {
                   return true;
               }
               else
               {
                   nMsg = "Sorry, you have no right to call the Web service ";
                   return false;
               }
           }
           catch
           {
               nMsg = "Sorry, you have no right to call the Web service";
               return false;
           }
       }
       public bool IsValid(out string nMsg)
       {
           return IsValid(userID,userPW,out nMsg);
       }
    }
}

好了 , 加入身份验证也是为了让服务更加的安全。

在服务中使用身份验证信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace WebEmpty
{
    /// <summary>
    /// WebAiny 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    //[System.Web.Script.Services.ScriptService]
    //[XmlInclude(typeof(DM_Introduce))]
    public class WebAiny : System.Web.Services.WebService
    {
        public Authentication_WS authentication = new Authentication_WS();//引入身份验证类
        //[OperationContract]
        //[WebGet(UriTemplate = "Add/{x}/{y}", ResponseFormat = WebMessageFormat.Xml)] 
        [WebMethod(Description="测试WebService")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        [SoapHeader("authentication")]
        public string Add(int a, int b)
        {
            string msg = "";
            if (!authentication.IsValid(out msg))
            {
                return msg;
            }
            else
            {
                return (a + b).ToString();
            }
        }
        [WebMethod(Description = "测试类型")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public void Type() {
            Context.Response.Write("OK - no SOAP xml Data"); 
        }
    }
}

(重点)注意点:

①Add方法使用了身份验证功能 , 所以此方法上需要加一个特性: [SoapHeader("authentication")]   ( authentication -》 public Authentication_WS authentication = new Authentication_WS();//引入身份验证类)


这个IIS web服务器配置,读者可以搜百度自己解决。运行程序如下:

C# WebService详解

我建了一个控制台程序来测试这个WebService。

是使用WebService的功能必须要引用WebService的服务,引用方法步骤如下所示:


①,引入WebService

C# WebService详解

C# WebService详解

C# WebService详解

C# WebService详解

如上图,我的WebService的引用名称为WS

②看测试代码 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

结果:

C# WebService详解

需要指出的是 : Authentication_WSValue属性是系统自动生成的(就是自己的身份验证类后面紧加一个Value),用于设置验证类。

我们给一个错误的账号(密码错误):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin1";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

结果为:

C# WebService详解

这样就能比较好的保护自己的服务了。。。。。。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI