自定义一个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> |