2023-05-23 16:13:17 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Web ;
using System.ServiceModel.Dispatcher ;
using System.Runtime.Serialization ;
using DeiNiu.Utils ;
using DeiNiu.wms.Data.Model ;
using System.Net ;
namespace WcfServiceAuthentication
{
public class AuthenticationInspector : IDispatchMessageInspector
{
2023-11-21 19:18:23 +08:00
// protected static log4net.ILog log = log4net.LogManager.GetLogger("logCommon");
2023-05-23 16:13:17 +08:00
public static int testUserId = 0 ;
public static Dictionary < int , string > authCach = new Dictionary < int , string > ( ) ;
2023-09-04 22:41:19 +08:00
public static Dictionary < int , int > tmpCodes = new Dictionary < int , int > ( ) ;
2024-05-21 17:28:35 +08:00
public static List < int > restrictUsers = new List < int > ( ) ;
2023-05-23 16:13:17 +08:00
static string [ ] publicServices = { "/Login.svc" , "/PortalService.svc" , "/MobileService.svc" , "/ScheduledService.svc" , "/android.svc/login" } ;
2024-05-21 17:28:35 +08:00
2023-05-23 16:13:17 +08:00
public object AfterReceiveRequest ( ref System . ServiceModel . Channels . Message request , System . ServiceModel . IClientChannel channel , System . ServiceModel . InstanceContext instanceContext )
{
//注意引用 System.Runtime.Serialization
string userIdKey = "UserId" ;
string tokenKey = "Token" ;
2023-09-04 22:41:19 +08:00
string tmpTokenKey = "tmpToken" ;
int tmpToken = 0 ;
2023-05-23 16:13:17 +08:00
int userId = 0 ;
// string Password = request.Headers.GetHeader<string>("Password", "www.test.com");
string token = "" ;
string requestPath = channel . LocalAddress . Uri . AbsolutePath ;
WebHeaderCollection headerCollection = System . ServiceModel . Web . WebOperationContext . Current . IncomingRequest . Headers ;
foreach ( string item in headerCollection )
{
if ( item = = userIdKey )
userId = Convert . ToInt32 ( headerCollection . Get ( item ) ) ;
if ( item = = tokenKey )
token = headerCollection . Get ( item ) ;
2023-09-04 22:41:19 +08:00
if ( item = = tmpTokenKey )
{
2023-11-21 19:18:23 +08:00
tmpToken = Convert . ToInt32 ( headerCollection . Get ( item ) ) ;
tmpCodes [ userId ] = tmpToken ;
2023-09-04 22:41:19 +08:00
}
2023-05-23 16:13:17 +08:00
}
if ( publicServices . Contains ( requestPath ) )
{
return null ;
}
string methdPath = request . Properties . Via . AbsolutePath ;
if ( publicServices . Contains ( methdPath ) )
{
return null ;
}
2023-12-03 22:13:49 +08:00
if ( userId = = 0 ) //for win client
2023-05-23 16:13:17 +08:00
{
2023-09-04 22:41:19 +08:00
try {
2023-05-23 16:13:17 +08:00
userId = request . Headers . GetHeader < int > ( "UserId" , "www.deinu.com" ) ;
// string Password = request.Headers.GetHeader<string>("Password", "www.test.com");
token = request . Headers . GetHeader < string > ( "Token" , "www.deinu.com" ) ;
2023-12-03 22:13:49 +08:00
2023-11-21 19:18:23 +08:00
}
catch ( System . ServiceModel . MessageHeaderException er )
2023-09-04 22:41:19 +08:00
{
}
2023-05-23 16:13:17 +08:00
}
2023-12-03 22:13:49 +08:00
2023-11-21 19:18:23 +08:00
2024-02-06 19:36:47 +08:00
// LogHelper.debug("svr AuthenticationInspector", string.Format("request: {2},set UserId : {0},set token: {1}, tmpToken: {3}", userId, token, request.ToString(), tmpToken));
2023-11-21 19:18:23 +08:00
// log.Debug( string.Format("svr AuthenticationInspector request: {2},set UserId : {0},set token: {1}, tmpToken: {3}", userId, token, request.ToString(),tmpToken));
2023-05-23 16:13:17 +08:00
if ( ! validUser ( userId , token ) ) // not in cache
{
2024-05-21 17:28:35 +08:00
LogHelper . debug ( "svr AuthenticationInspector auth error" , string . Format ( "request: {2}, UserId : {0}, token: {1}" , userId , token , request . ToString ( ) ) ) ;
2023-05-23 16:13:17 +08:00
throw new DeiNiuTimeOutException ( WmsConstants . WCF_UN_AUTH_MESSAGE ) ;
2023-09-04 22:41:19 +08:00
}
if ( tmpToken > 0 )
{
tmpCodes [ userId ] = tmpToken ;
}
2023-05-23 16:13:17 +08:00
return null ;
}
public void BeforeSendReply ( ref System . ServiceModel . Channels . Message reply , object correlationState )
{
string res = reply . ToString ( ) ;
return ;
}
public static bool validUser ( int userId , string token )
{
2024-05-21 17:28:35 +08:00
if ( WmsConstants . IS_ONLINE_RESTRICT & & restrictUsers . Count > = WmsConstants . MAX_ONLINE_USERS & & ! restrictUsers . Contains ( userId ) )
2023-05-23 16:13:17 +08:00
{
2024-05-21 17:28:35 +08:00
LogHelper . debug ( "AuthenticationInspector" , string . Format ( " userId {0} is not in the allowed list, current list size {1}, maxSize {2} " , userId , restrictUsers . Count , WmsConstants . MAX_ONLINE_USERS ) ) ;
return false ;
2023-05-23 16:13:17 +08:00
}
2024-05-21 17:28:35 +08:00
//
if ( ! authCach . ContainsKey ( userId ) ) //检查内存
2023-05-23 16:13:17 +08:00
{
2024-05-21 17:28:35 +08:00
LogHelper . debug ( "AuthenticationInspector" ,
string . Format ( "userId {0} NOT in the cache, to check db..." , userId ) ) ;
//检查db
Employee em = new Employee ( ) ;
try
2023-05-23 16:13:17 +08:00
{
2024-05-21 17:28:35 +08:00
if ( em . login ( token ) & & em . ID = = userId )
{
LogHelper . debug ( "AuthenticationInspector" , string . Format ( "valid token {0} in the db and token match " , token ) ) ;
authCach [ userId ] = token ;
}
else
{
LogHelper . debug ( "AuthenticationInspector" ,
string . Format ( "userId {0} and token NOT match db...return false " , userId ) ) ;
return false ;
}
2023-05-23 16:13:17 +08:00
}
2024-05-21 17:28:35 +08:00
catch
{
LogHelper . debug ( "AuthenticationInspector" ,
string . Format ( " exception happened ...return false " , userId ) ) ;
return false ;
}
2023-05-23 16:13:17 +08:00
}
2024-05-21 17:28:35 +08:00
if ( authCach [ userId ] = = token )
2023-05-23 16:13:17 +08:00
{
2024-05-21 17:28:35 +08:00
if ( WmsConstants . IS_ONLINE_RESTRICT )
{
checkRestrictUsers ( userId ) ;
}
return true ;
2023-05-23 16:13:17 +08:00
}
2024-05-21 17:28:35 +08:00
2023-05-23 16:13:17 +08:00
return false ;
2024-05-21 17:28:35 +08:00
2023-05-23 16:13:17 +08:00
}
2024-05-21 17:28:35 +08:00
public static void checkRestrictUsers ( int userId )
{
if ( userId < = 0 )
{
return ;
}
2024-05-24 19:11:48 +08:00
//
2024-05-21 17:28:35 +08:00
if ( ! AuthenticationInspector . restrictUsers . Contains ( userId ) )
{
if ( AuthenticationInspector . restrictUsers . Count > = WmsConstants . MAX_ONLINE_USERS )
{
2024-05-24 19:11:48 +08:00
LogHelper . debug ( "1 checkRestrictUsers" , string . Format ( " checking ..userId {0} , current list size {1}, maxSize {2} " , userId , restrictUsers . Count , WmsConstants . MAX_ONLINE_USERS ) ) ;
2024-05-21 17:28:35 +08:00
LogHelper . debug ( "checkRestrictUsers" , string . Format ( " to remove ..userId {0} " , AuthenticationInspector . restrictUsers [ 0 ] ) ) ;
AuthenticationInspector . restrictUsers . RemoveAt ( 0 ) ;
}
2024-05-24 19:11:48 +08:00
2024-05-21 17:28:35 +08:00
AuthenticationInspector . restrictUsers . Add ( userId ) ;
2023-05-23 16:13:17 +08:00
2024-05-21 17:28:35 +08:00
foreach ( int i in AuthenticationInspector . restrictUsers )
{
LogHelper . debug ( "checkRestrictUsers" , string . Format ( " active userId {0} " , i ) ) ;
}
}
/ *
LogHelper . debug ( "2 checkRestrictUsers" , string . Format ( " checking ..userId {0} , current list size {1}, maxSize {2} " , userId , restrictUsers . Count , WmsConstants . MAX_ONLINE_USERS ) ) ;
foreach ( int i in AuthenticationInspector . restrictUsers )
{
LogHelper . debug ( "checkRestrictUsers" , string . Format ( " active userId {0} " , i ) ) ;
}
* /
}
2023-05-23 16:13:17 +08:00
}
}