//相当于接口帐号密码
const string strApiCorpId = "XXXXXXXX";
const string strApiCorpSecret = "XXXXXXXX";
const string strApiUriHead = "https://oapi.dingtalk.com/";
static CookieContainer cookieCon = new CookieContainer();
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"> DateTime时间格式</param>
/// <returns>Unix时间戳格式</returns>
public int ConvertDateTimeInt(DateTime time)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
//获取token
public string GetToken()
{
if (HttpContext.Current.Session["token"]==null)
{
const string strApiTokenUriFormat =
strApiUriHead + "gettoken/?corpid=" + strApiCorpId + "&corpsecret=" + strApiCorpSecret;
//获得access_token
string strJson = HTMLHelper.GetHtml(strApiTokenUriFormat, cookieCon);
TokenUserInfo info = ExpandFunction.ToInfo<TokenUserInfo>(strJson);
HttpContext.Current.Session["token"] = info.access_token;
}
return CommonFunction.ToStr(HttpContext.Current.Session["token"]);
}
// 获取dd.config参数值
public string GetDingDingConfig(string strUrl, string strQueryurl)
{
string strToken = GetToken();
string strNowTime = CommonFunction.ToStr(ConvertDateTimeInt(DateTime.Now));
if (HttpContext.Current.Session["ticket"] == null)
{
const string strApiGetJsApiTicket = strApiUriHead + "get_jsapi_ticket?access_token={0}&type=jsapi";
string strJsapiJson = HTMLHelper.GetHtml(string.Format(strApiGetJsApiTicket, strToken), cookieCon);
JsapiticketInfo ticketinfo = ExpandFunction.ToInfo<JsapiticketInfo>(strJsapiJson);
HttpContext.Current.Session["ticket"] = ticketinfo.ticket;
}
string strticket = CommonFunction.ToStr(HttpContext.Current.Session["ticket"]);
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("noncestr", "asdfg");
dic.Add("timestamp", strNowTime);
dic.Add("jsapi_ticket", strticket);
dic.Add("url", "页面地址,需要跟微应用后台配置的微应用网址完全一样 包括大小写");
var list = dic.OrderBy(s => s.Key);
string param = "";
foreach (var s in list)
param += s.Key + "=" + s.Value + "&";
param = param.Trim(new char[] { '&' });
string strSignTrue = sha1(param);
return "{'appId':'XXXXXX','corpId':'XXXXXX','timeStamp':'" + strNowTime + "','nonceStr':'asdfg','ticketinfo':'" + strticket + "','signature':'" + strSignTrue + "','access_token':'" + strToken + "'}";
}
//sha1 加密
private static string sha1(string strSignTrue)
{
//建立SHA1对象
SHA1 sha = new SHA1CryptoServiceProvider();
//将mystr转换成byte[]
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(strSignTrue);
//Hash运算
byte[] dataHashed = SHA1.Create().ComputeHash(dataToHash );
//将运算结果转换成string
string hash = BitConverter.ToString(dataHashed).Replace("-", "").ToLower();
return hash;
}
-------------------------
-------------------------
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。