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>();
|
2023-05-23 16:13:17 +08:00
|
|
|
|
|
|
|
|
|
static string[] publicServices = { "/Login.svc", "/PortalService.svc", "/MobileService.svc", "/ScheduledService.svc", "/android.svc/login" };
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
LogHelper.debug("svr AuthenticationInspector auth error", string.Format("request: {2},set UserId : {0},set token: {1}", userId, token, request.ToString()));
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-09-04 22:41:19 +08:00
|
|
|
|
//
|
2023-05-23 16:13:17 +08:00
|
|
|
|
if (authCach.ContainsKey(userId)) //检查内存
|
|
|
|
|
{
|
2024-03-06 20:48:05 +08:00
|
|
|
|
// LogHelper.debug("AuthenticationInspector", string.Format("get userId {0} in the cache checking token {1},token match? {2}", userId, token, authCach[userId].Equals(token)));
|
2023-05-23 16:13:17 +08:00
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
|
|
if (!authCach[userId].Equals(token))
|
|
|
|
|
{
|
|
|
|
|
Employee em1 = new Employee();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (em1.login(token) && em1.ID == userId)
|
|
|
|
|
{
|
|
|
|
|
LogHelper.debug("AuthenticationInspector", string.Format("valid token {0} in the db and token match ", token));
|
|
|
|
|
authCach[userId] = token;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return authCach[userId].Equals(token);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查db
|
|
|
|
|
Employee em = new Employee();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|