/// <summary> /// 验证Token(并刷新) /// </summary> /// <param name="ticket"></param> /// <param name="ticketModel">票据实体</param> /// <returns></returns> public bool ValidateTicket(string ticket, out AuthenticationTicketDetailsModel ticketModel) { var helper = new AuthenticationDataHelper(); ticketModel = helper.GetCheckTicket(ticket); if (ticketModel.IsNull()) { return(false); } //后面可以增加 http 头部 增加 appid、clientType、uuid //if (ticketModel.TicketAppID) //{ //} if (ticketModel.LastRefreshDate.AddSeconds(ticketModel.TicketSecond) < DateTime.Now) { return(false); } ////验证票据设备号是否相符 //if (ticketModel.DeviceNo != AuthenticationHelper.GetDecryptTicket(ticket).DeviceNo) //{ // return false; //} helper.RefreshTicketDate(ticket); return(true); }
/// <summary> /// 验证签名 /// </summary> /// <param name="ticket"></param> /// <param name="dictionary"></param> /// <returns></returns> private BusinessBaseViewModel <string> ValidateSignature(string ticket, Dictionary <string, object> dictionary) { BusinessBaseViewModel <string> response = new BusinessBaseViewModel <string> { Status = ResponseStatus.Fail }; string appSecret = string.Empty; if (string.IsNullOrEmpty(ticket)) { if (!dictionary.ContainsKey("AppId")) { response.Status = ResponseStatus.ParameterError; return(response); } var appModel = new AuthenticationDataHelper().GetApplocationAuthorModel(dictionary["AppId"].ToString()); appSecret = appModel.AppSecret; } else { var ticketModel = AuthenticationHelper.GetDecryptTicket(ticket);// new AuthenticationDataHelper().GetApplocationAuthorModel(ticket); appSecret = ticketModel.AppSecret; } if (!dictionary.ContainsKey(signKey)) { response.Status = ResponseStatus.UnSignatureParamsError; return(response); } string signature = dictionary[signKey].ToString(); dictionary.Remove(signKey); //验证签名 string codesign = AuthenticationHelper.GetAuthenticationCode(dictionary, appSecret); if (!signature.Equals(codesign, StringComparison.CurrentCultureIgnoreCase)) { bool flag = HttpContext.Current.Request.Url.Host.Equals("localhost", StringComparison.CurrentCultureIgnoreCase); response.Status = ResponseStatus.UnSignatureError; response.BusinessData = flag ? codesign : ""; return(response); } //验证时效性 if (!dictionary.ContainsKey("Timestamp")) { response.Status = ResponseStatus.UnTimeSpanFromatError; return(response); } response.Status = AuthenticationHelper.CheckTimeStamp(dictionary["Timestamp"].ToString()); return(response); }
/// <summary> /// 生成票据 /// </summary> /// <param name="bodyModel"></param> /// <returns></returns> public static BusinessBaseViewModel <string> BuildToken(ApiAuthorizeRequestModel bodyModel) { var response = new BusinessBaseViewModel <string>() { Status = ResponseStatus.Fail }; if (string.IsNullOrEmpty(bodyModel.AppId) || string.IsNullOrEmpty(bodyModel.Timestamp) || string.IsNullOrEmpty(bodyModel.ClientType) || string.IsNullOrEmpty(bodyModel.DeviceNo) || string.IsNullOrEmpty(bodyModel.Noncestr) || string.IsNullOrEmpty(bodyModel.Signature)) { response.ErrorMessage = "请求参数错误"; return(response); } Dictionary <string, object> dictionary = new Dictionary <string, object> { { "AppId", bodyModel.AppId }, { "Timestamp", bodyModel.Timestamp }, { "Noncestr", bodyModel.Noncestr }, { "DeviceNo", bodyModel.DeviceNo }, { "ClientType", bodyModel.ClientType } }; var authenticationData = new AuthenticationDataHelper(); var applocationAuthor = authenticationData.GetApplocationAuthorModel(bodyModel.AppId); if (applocationAuthor == null) { response.ErrorMessage = "不存在的app"; return(response); } if (!applocationAuthor.ClientType.Equals(bodyModel.ClientType, StringComparison.CurrentCultureIgnoreCase)) { response.ErrorMessage = "请求APP客户端错误"; return(response); } string parms = string.Empty; string codesign = GetAuthenticationCode(dictionary, applocationAuthor.AppSecret, out parms); if (!bodyModel.Signature.Equals(codesign, StringComparison.CurrentCultureIgnoreCase)) { if (HttpContext.Current.Request.Url.Host.Contains("localhost")) { response.BusinessData = (new { sign = codesign, md5String = parms }).ToJsonString(); } response.ErrorMessage = "参数签名错误"; return(response); } response.Status = CheckTimeStamp(bodyModel.Timestamp); if (response.Status != ResponseStatus.Success) { return(response); } //生成票据 var ticket = GetEncryptTicket(bodyModel.AppId, bodyModel.ClientType, bodyModel.DeviceNo, applocationAuthor.AppSecret); var model = authenticationData.GetCheckTicket(ticket); if (model.IsNull()) { authenticationData.AddAuthenticationTicketDetails(new FilterAttribute.AuthorizeCode.AuthenticationTicketDetailsModel() { Ticket = ticket, TicketAppID = bodyModel.AppId, TicketId = Utils.NewGuid(), LastRefreshDate = DateTime.Now, TicketSecond = 7200,//单位秒,默认7200秒 DeviceNo = bodyModel.DeviceNo, ClientType = bodyModel.ClientType, CreateTime = DateTime.Now, AppSecret = applocationAuthor.AppSecret }); } response.BusinessData = ticket; response.Status = ResponseStatus.Success; return(response); }