using System; using System.Collections.Generic; using System.Linq; using System.Text; using Bowin.Common.Utility; using System.Text.RegularExpressions; using System.Globalization; namespace Bowin.Common.ServiceToken { [Serializable] public class TokenCombiner { public static TokenView CreateToken(string systemCode, string password) { var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); TokenView result = new TokenView(); var validateString = systemCode + password + timeStamp; result.Token = validateString.MD5(); result.TimeStamp = timeStamp; return result; } private static TokenError CheckTimeStamp(string timeStamp) { DateTime dt; if (!DateTime.TryParse(timeStamp, out dt)) { if (!DateTime.TryParseExact(timeStamp, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None, out dt)) return new TokenError(-97, "时间戳格式错误"); } var ts = DateTime.Now - dt; if (ts.TotalMinutes >= 15 || ts.TotalMinutes < -1) { return new TokenError(-98, "令牌超时。"); } return null; } public static TokenError CheckTokenBySystemCode(string token, string systemCode, string timeStamp, string sysPwd) { var tokenError = CheckTimeStamp(timeStamp); if (tokenError != null) { return tokenError; } if (systemCode == null) return new TokenError(-97, "系统代码验证失败"); var validateString = systemCode + sysPwd + timeStamp; var md5String = validateString.MD5(); if (md5String.ToUpper() != token.ToUpper()) { return new TokenError(-97, "令牌无效"); } else { return null; } } } }