.net core发送钉钉消息_消息加签模式
作者:互联网
这是一篇纯记录代码文章,完整运行需要自己调整:
钉钉发送消息后台进行了改版,这里介绍加签模式的代码:
/// <summary> /// 发送钉钉消息内容 /// </summary> /// <param name="groupName"></param> /// <param name="content"></param> /// <returns></returns> public async Task SendDingdingMessage(string content) { return;//去掉钉钉消息通知 string requestHost = this._httpContextAccessor.HttpContext.Request.Host.Value; //测试和仿真环境发钉钉提示消息 if (requestHost.Contains("meshop.net") || requestHost.Contains("runshopstore.com")) { string sign = null; TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); string timeStamp = Convert.ToInt64(ts.TotalMilliseconds).ToString(); var encoding = new UTF8Encoding(); byte[] signBytes = encoding.GetBytes($"{timeStamp}\n{this._dingDingGroup.Secret}"); byte[] secretByte = encoding.GetBytes(this._dingDingGroup.Secret); using (var hmacsha256 = new HMACSHA256(secretByte)) { byte[] hashmessage = hmacsha256.ComputeHash(signBytes); sign = HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage)); } string messageUrl = $"{this._dingDingGroup.WebHookUrl}×tamp={timeStamp}&sign={sign}"; List<string> atPhoneList = null; if (!string.IsNullOrEmpty(this._dingDingGroup.AtPhones)) { atPhoneList = this._dingDingGroup.AtPhones.Split(',').ToList(); atPhoneList.RemoveAll(m => string.IsNullOrEmpty(m)); } DingDingText dingDingText = new DingDingText { msgtype = "text", text = new DingDingText.textObj { content = $"{CONST.HOST_ADMIN}_{content}" }, at = new DingDingText.atObj { isAtAll = false, atMobiles = atPhoneList } }; HttpClient httpClient = this._httpClientFactory.CreateClient(); await httpClient.Post(messageUrl, JsonHelper.ConvertJsonToStr(dingDingText), "application/json"); } }
标签:core,string,dingDingGroup,sign,atPhoneList,._,new,net,加签 来源: https://www.cnblogs.com/lxhbky/p/14415440.html