zoukankan      html  css  js  c++  java
  • Asp.Net WebService 使用后来管理系统对接口方法进行公开控制

    思路:

      1、需要找一个访问Webservice的统一入口,刚开始进入了一个误区,以为WebService是单独的运行程序,后来经朋友提醒,其实它也是通过http请求在asp.net framework中的处理流程,所以就可以使用HttpModule作为统一入口,进行判断处理。
      2、进入统一入口后,就需要获得访问Webservice的接口方法的名称,然后在网友提供的答案中:Request 的 RequestUri(GET)或 Headers(POST)来得到客户端请求的 WebMethod 名称,并参考一些文章,就实现获得接口方法的名称,就可以连接数据库进行判断处理了。

    实现代码:

      自定义一个HttpModule类,实现IHttpModule接口

    /// <summary>
        /// 验证调用的接口方法是否公开
        /// </summary>
        public sealed class WebServiceVerifyModule : IHttpModule
        {
            //销毁不再被HttpModule使用的资源
            public void Dispose()
            {
            }
    
            //初始化一个Module,为捕获HttpRequest做准备
            public void Init(HttpApplication context)
            {
                context.BeginRequest += new EventHandler(Application_BeginRequest);
            }
    
            //处理请求的方法
            public void Application_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication application = sender as HttpApplication;
                HttpRequest request = application.Request;
                string requestMethod = "";
                if (request.UrlReferrer != null && !string.IsNullOrEmpty(request.UrlReferrer.AbsolutePath))
                {
                    //web端调试即出期望结果
                    requestMethod = request.Url.AbsoluteUri.Split(new char[] { '/' }).Last();
                }
                else
                {
                    //由winform client 调用 需要用此方法
                    if (request["HTTP_SOAPACTION"] != null)
                    {
                        requestMethod = request["HTTP_SOAPACTION"].Replace(""", "").Split(new char[] { '/' }).Last();
    
                    }
                }
    
                //连接数据库进行查询判断
                if (!string.IsNullOrEmpty(requestMethod))
                {
                    string sql = "select * from OPRT_APIINFO where F_METHOD='" + requestMethod + "'";
                    DataTable dt = DbHelperOra.Query(sql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        string isOpen = dt.Rows[0]["F_ISOPEN"].ToString();
                        if (isOpen != "1")
                        {
                            application.CompleteRequest();
                            application.Context.Response.Write("{{"result":"401","接口方法未被公开!":"{1}"}}");
                        }
                    }
                }
            }
        }

      在Web.Config中配置:

    <system.web>
        <httpModules>
          <add name="WebServiceVerifyModule" type="WebServiceDemo.WebServiceVerifyModule"/>
        </httpModules>
    </system.web>

    总结:

      要解决一个问题,首先需要有一个思路,然后要熟悉一些相关的原理知识,这样解决问题时就会游刃有余。

    参考文章:

    HttpModule的认识

    如何:执行使用 SOAP 标头的自定义身份验证

    .Net WebService中 如何从Request对象中获取请求的方法和传入参数

     

  • 相关阅读:
    vue-cli快速搭建
    js严格模式下判断数据类型
    js实现本地的图片压缩上传预览
    web端实现图片放大切换显示预览
    swiper.js在隐藏/显示切换时,轮播出现bug的解决办法
    Zepto.js实现fadeIn,fadeOut功能
    ms
    redis 解决秒杀
    单下滑线 事务 锁
    极验
  • 原文地址:https://www.cnblogs.com/hdkn235/p/3485567.html
Copyright © 2011-2022 走看看