Previous topicNext topic
Help > 开发指南 > API对接 > HttpServer > 事件 >
中间件示例-IP白名单

 

Vb.Net
Dim e As SmHttpRequestEventArgs=Args(0)

'IP白名单
If Proj.ConcurrentDictionary.Count<=0 Then
    '将IP的集合存放到Proj.ConcurrentDictionary
    Proj.ConcurrentDictionary.Clear()
    Dim lst As List(Of String)=Proj.SysDataFactory("UserDB").GetListOfColumnValue("select IpAddress from IpWhiteList")
    For Each ipAddress As String In lst
        Proj.ConcurrentDictionary.TryAdd(ipAddress,"")
    Next
End If
If Proj.ConcurrentDictionary.Count>0 Then '如果有IP白名单,则认为有需要控制IP访问
    Dim strIp As String
    '如果没有在白名单找到相应的IP,则返回False
    If Not Proj.ConcurrentDictionary.TryGetValue(e.RemoteAddress,strIp) Then
        Return False
    End If  
End If

'中间件如果不想后续程序的执行的话,则必须返回True
Return True

C#
SmHttpRequestEventArgs e = (SmHttpRequestEventArgs)Args[0];

// IP白名单
if (Proj.ConcurrentDictionary.Count <= 0)
{
    // 将IP的集合存放到Proj.ConcurrentDictionary
    Proj.ConcurrentDictionary.Clear();
    List<string> lst = Proj.SysDataFactory("UserDB").GetListOfColumnValue("select IpAddress from IpWhiteList");
    foreach (string ipAddress in lst)
    {
        Proj.ConcurrentDictionary.TryAdd(ipAddress, "");
    }
}
if (Proj.ConcurrentDictionary.Count > 0) // 如果有IP白名单,则认为有需要控制IP访问
{
    string strIp;
    // 如果没有在白名单找到相应的IP,则返回False
    if (!Proj.ConcurrentDictionary.TryGetValue(e.RemoteAddress, out strIp))
    {
        return false;
    }
}

// 中间件如果不想后续程序的执行的话,则必须返回True
return true;
< !--EndFragment-- >