原文:钉钉H5w微应用鉴权
1 ///// <summary> 2 ///// 登录获取签名 3 ///// </summary> 4 ///// <param name = "url" > 获取的url </ param > 5 ///// < returns > 成功时,返回签名信息</returns> //引用system.web 6 public signResultObject DDGetSign(string url) 7 { 8 try 9 { 10 String jsTicket = get_jsapi_ticket(); 11 if (jsTicket != "") 12 { 13 String noncestr = GetRandomString(20, true, true, true, false, ""); 14 String jsUrl = HttpUtility.UrlDecode(url);//http://www.test.com/DDh5/index.html 15 int timeStamp = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); 16 String plainTex = "jsapi_ticket=" + jsTicket + "&noncestr=" + noncestr + "×tamp=" + timeStamp.ToString() + "&url=" + jsUrl; 17 object SignObj = new object(); 18 SignObj = new 19 { 20 corpId = dingConst.Corpid, 21 nonceStr = noncestr, 22 timeStamp = timeStamp, 23 signature = EncryptHelper.Sha1(plainTex).ToLower() 24 }; 25 return new signResultObject { result = SignObj, code = 0, message = "获取成功!" }; 26 } 27 else 28 { 29 return new signResultObject { result = null, code = 9, message = "获取Ticket失败!" }; 30 } 31 } 32 catch (Exception ex) 33 { 34 35 return new signResultObject { result = null, code = 10, message = "获取Ticket出现异常!" }; 36 } 37 }
1 public static string get_jsapi_ticket() 2 { 3 DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/get_jsapi_ticket"); 4 OapiGetJsapiTicketRequest req = new OapiGetJsapiTicketRequest(); 5 req.SetHttpMethod("GET"); 6 OapiGetJsapiTicketResponse execute = client.Execute(req, getAccessTokenH5Trans()); 7 return execute.Ticket;//注意:ticket不对,jsap鉴权会失败 8 }
1 public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) 2 { 3 byte[] b = new byte[4]; 4 new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); 5 Random r = new Random(BitConverter.ToInt32(b, 0)); 6 string s = null, str = custom; 7 if (useNum == true) { str += "0123456789"; } 8 if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } 9 if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } 10 if (useSpe == true) { str += "!\"#$%&‘()*+,-./:;<=>[email protected][\\]^_`{|}~"; } 11 for (int i = 0; i < length; i++) 12 { 13 s += str.Substring(r.Next(0, str.Length - 1), 1); 14 } 15 return s; 16 }//生成签名的随机串
1 using System; 2 using System.Security.Cryptography; 3 using System.Text; 4 namespace Test.Code 5 { 6 public static class EncryptHelper 7 { 8 /// <summary> 9 /// 基于Md5的自定义加密字符串方法:输入一个字符串,返回一个由32个字符组成的十六进制的哈希散列(字符串)。 10 /// </summary> 11 /// <param name="str">要加密的字符串</param> 12 /// <returns>加密后的十六进制的哈希散列(字符串)</returns> 13 public static string Md5(this string str) 14 { 15 //将输入字符串转换成字节数组 16 var buffer = Encoding.Default.GetBytes(str); 17 //接着,创建Md5对象进行散列计算 18 var data = MD5.Create().ComputeHash(buffer); 19 20 //创建一个新的Stringbuilder收集字节 21 var sb = new StringBuilder(); 22 23 //遍历每个字节的散列数据 24 foreach (var t in data) 25 { 26 //格式每一个十六进制字符串 27 sb.Append(t.ToString("X2")); 28 } 29 30 //返回十六进制字符串 31 return sb.ToString(); 32 } 33 34 /// <summary> 35 /// 基于Sha1的自定义加密字符串方法:输入一个字符串,返回一个由40个字符组成的十六进制的哈希散列(字符串)。 36 /// </summary> 37 /// <param name="str">要加密的字符串</param> 38 /// <returns>加密后的十六进制的哈希散列(字符串)</returns> 39 public static string Sha1(this string str) 40 { 41 var buffer = Encoding.UTF8.GetBytes(str); 42 var data = SHA1.Create().ComputeHash(buffer); 43 44 var sb = new StringBuilder(); 45 foreach (var t in data) 46 { 47 sb.Append(t.ToString("X2")); 48 } 49 50 return sb.ToString(); 51 } 52 } 53 }
原文地址:https://www.cnblogs.com/lonelyxmas/p/12408965.html
时间: 2024-10-03 10:44:36