112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Runtime.Serialization;
|
||
using System.ServiceModel;
|
||
using System.ServiceModel.Web;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
using WX.CRM.Model.DTO;
|
||
using DAL.Redis;
|
||
using Newtonsoft.Json;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.DAL;
|
||
using WX.CRM.DAL.Redis;
|
||
using WX.CRM.Model.Entity;
|
||
using WX.CRM.Model.Enum;
|
||
using WX.CRM.IBLL.Wx;
|
||
using WX.CRM.WebHelper.Infrastructure;
|
||
using Ninject;
|
||
using WX.CRM.DataSynFactory;
|
||
using WX.CRM.IBLL.Base;
|
||
using WX.CRM.Model.MAP;
|
||
using WX.CRM.Model.weapp;
|
||
using WX.CRM.IBLL.weapp;
|
||
using System.Web.Security;
|
||
namespace AppletService
|
||
{
|
||
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WxMessageSvr”。
|
||
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WxMessageSvr.svc 或 WxMessageSvr.svc.cs,然后开始调试。
|
||
public class WxMessageSvr : IWxMessageSvr
|
||
{
|
||
|
||
private IAppletMsg appletmsg = NinjectControllerFactory.ninjectKernel.Get<IAppletMsg>();
|
||
public string AppletMsg(AppletMsgModel appletMsg, string signature, string timestamp, string nonce)
|
||
{
|
||
|
||
LogHelper.Info(string.Format("signature:{0},timestamp:{1},nonce{2}", signature, timestamp, nonce));
|
||
if (!IsFromWxServ(signature, timestamp, nonce))
|
||
{
|
||
LogHelper.Info("验证失败");
|
||
return "fail";
|
||
}
|
||
LogHelper.Info("验证成功");
|
||
//LogHelper.Info(string.Format("FromUserName:{0},MsgId:{1},MsgType:{2},ToUserName:{3},Content:{4}", appletMsg.FromUserName, appletMsg.MsgId, appletMsg.MsgType, appletMsg.ToUserName, appletMsg.Content));
|
||
bool issucced = appletmsg.WeapAddMessage(appletMsg);
|
||
if (issucced)
|
||
return "success";
|
||
else
|
||
return "fail";
|
||
}
|
||
private const string Token = "wxapplet_es6WcS8";
|
||
private bool IsFromWxServ(string signature, string timestamp, string nonce)
|
||
{
|
||
|
||
var strArray = new string[] { nonce, timestamp, Token };
|
||
Array.Sort(strArray);
|
||
string strContent = string.Join("", strArray);
|
||
strContent = EncryptSHA1(strContent);//进行sha1加密
|
||
strContent = strContent.ToLower();
|
||
if (signature == strContent)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
public static string EncryptSHA1(string value)
|
||
{
|
||
if (value == null || value.Trim() == "")
|
||
{
|
||
return string.Empty;
|
||
}
|
||
else
|
||
{
|
||
return FormsAuthentication.HashPasswordForStoringInConfigFile(value, "SHA1");
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
[ServiceContract]
|
||
public interface IWxMessageSvr
|
||
{
|
||
|
||
[OperationContract]
|
||
[WebInvoke(Method = "POST",
|
||
RequestFormat = WebMessageFormat.Json,
|
||
ResponseFormat = WebMessageFormat.Json,
|
||
BodyStyle = WebMessageBodyStyle.Bare,
|
||
UriTemplate = "AppletMsg/Put?signature={signature}×tamp={timestamp}&nonce={nonce}")]
|
||
string AppletMsg(AppletMsgModel appletMsg, string signature, string timestamp, string nonce);
|
||
}
|
||
|
||
}
|
||
|
||
[DataContract]
|
||
public class JsonResult<T>
|
||
{
|
||
[DataMember]
|
||
public bool result { get; set; }
|
||
[DataMember]
|
||
public int retcode { get; set; }
|
||
[DataMember]
|
||
public T retmsg { get; set; }
|
||
}
|
||
|