黑马程序员技术交流社区
标题:
Asp.Net WebService 使用后来管理系统对接口方法进行公开控制
[打印本页]
作者:
shangxin
时间:
2014-3-16 22:24
标题:
Asp.Net WebService 使用后来管理系统对接口方法进行公开控制
自定义一个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>
作者:
许庭洲
时间:
2014-3-17 07:58
值得学习ing!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2