C# 钉钉api接口开发(三) 免登录及自动刷新AccessToken

每次打开微应用时,需要登录,根据接口文档做免登录处理.


获取 AccessToken 根据 AccessToken去获取免登录 Ticket 然后签名,部分字段返回前台交给钉钉效验,通过之后则返回 code 根据 code 获取用户编码 通过用户编码获取用户信息.


唯一碰到的坑就是 签名的结果需要小写.


贴代码  前面的也贴出来好了 这个是钉钉demo接口的实现代码


public static class DdHelper
    {
        private static String dd_autoaddress = ConfigurationManager.AppSettings["DD_autoaddress"];
        private static String dd_host = ConfigurationManager.AppSettings["DD_host"];
        private static String dd_corpid = ConfigurationManager.AppSettings["DD_corpid"];
        private static String dd_corpsecret = ConfigurationManager.AppSettings["DD_corpsecret"];
        private static String dd_accesstoken = string.Empty;
        private static DateTime dd_accesstokentime;
        public static String jsTicket = string.Empty;
        public static String nonce = string.Empty;
        public static String jsUrl = string.Empty;
        public static int timeStamp = 0;

        /// 
        /// 免登录签名
        /// 
        /// 
        public static String GetSign() {
            jsTicket = GetTicket();
            nonce = "ERFGYHSKHSGHERUTH56460srteh60846dagh=G";
            timeStamp = Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
            jsUrl = dd_autoaddress;
            String plainTex = "jsapi_ticket=" + jsTicket + "&noncestr=" + nonce + "&timeStamp=" + timeStamp + "&url=" + jsUrl;
            return BLL.Lib.EncryptUtil.Sha1(plainTex).ToLower();
        }

        /// 
        /// 发起请求
        /// 
        /// 地址
        /// 数据
        /// 请求类型
        /// 
        private static String Request(string url,string data,string reqtype) {
                GetAccessToken();
                if (url.IndexOf('?') == -1 && url != "gettoken")
                    url += ("?access_token=" + dd_accesstoken);
                else if (url.IndexOf('?') > -1 && url.IndexOf("gettoken") == -1)
                    url += ("&access_token=" + dd_accesstoken);
            HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(dd_host + url);
            web.ContentType = "application/json";
            web.Method = reqtype;            
            if (data.Length > 0 && reqtype.Trim().ToUpper() == "POST")
            {
                byte[] postBytes = Encoding.UTF8.GetBytes(data);
                web.ContentLength = postBytes.Length;
                using (Stream reqStream = web.GetRequestStream())
                {
                    reqStream.Write(postBytes, 0, postBytes.Length);
                }
            }
            string html = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)web.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                html = streamReader.ReadToEnd();
            }
            return html;
        }
        /// 
        /// 更新AccessToken
        /// 
        public static void GetAccessToken()
        {
            if (dd_accesstokentime == null || (DateTime.Now.Ticks - dd_accesstokentime.Ticks) >= 5000)
            {
                dd_accesstokentime = DateTime.Now;
                dd_accesstoken = JsonConvert.DeserializeObject(Request("gettoken?corpid=" + dd_corpid + "&corpsecret=" + dd_corpsecret, "", "GET")).Access_Token;
            }
        }
        /// 
        /// 获取免登录令牌
        /// 
        /// 
        public static String GetTicket() {
            string json = Request("get_jsapi_ticket?type=jsapi", "", "GET");
            JObject jo = JsonConvert.DeserializeObject(json);
            if (Convert.ToInt32(jo["errcode"]) == 0)
                return jo["ticket"].Value();
            return "";
        }
        /// 
        /// 获取部门列表
        /// 
        /// 上级部门编号 允许为空
        /// 
        public static String GetDeptList(String id="") {
           return Request("department/list", "", "GET");
        }
        /// 
        /// 获取免登录的用户信息
        /// 
        /// 
        /// 
        public static String GetUserInfo(string code) {
            string json = Request("user/getuserinfo?code=" + code, "", "GET");
            BLL.Config.ConfigBll.InsertLog("获取免登录的用户信息", json, "GetUserInfo");
            JObject jo = JsonConvert.DeserializeObject(json);
            if (Convert.ToInt32(jo["errcode"]) == 0)
            {                
                string userid = jo["userid"].Value();
                json = Request("user/get?userid=" + userid, "", "GET");
                BLL.Config.ConfigBll.InsertLog("获取免登录的用户信息", json, "GetUserInfo");
            }
            return json;
        }
    }


发代码要发全


 public static class EncryptUtil
    {
        #region MD5加密

        /// 
        /// MD5加密
        /// 
        public static string Md532(this string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }

            var encoding = Encoding.UTF8;
            MD5 md5 = MD5.Create();
            return HashAlgorithmBase(md5, value, encoding);
        }

        /// 
        /// 加权MD5加密
        /// 
        public static string Md532(this string value, string salt)
        {
            return salt == null ? value.Md532() : (value + "『" + salt + "』").Md532();
        }

        #endregion

        #region SHA 加密

        /// 
        /// SHA1 加密
        /// 
        public static string Sha1(this string value)
        {
            if (value == null)
                throw new ArgumentNullException("需要加密的字符串不能为空。");

            var encoding = Encoding.UTF8;
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            return HashAlgorithmBase(sha1, value, encoding);
        }

        /// 
        /// SHA256 加密
        /// 
        public static string Sha256(this string value)
        {
            if (value == null)
                throw new ArgumentNullException("需要加密的字符串不能为空。");


            var encoding = Encoding.UTF8;
            SHA256 sha256 = new SHA256Managed();
            return HashAlgorithmBase(sha256, value, encoding);
        }

        /// 
        /// SHA512 加密
        /// 
        public static string Sha512(this string value)
        {
            if (value == null)
                throw new ArgumentNullException("需要加密的字符串不能为空。");

            var encoding = Encoding.UTF8;
            SHA512 sha512 = new SHA512Managed();
            return HashAlgorithmBase(sha512, value, encoding);
        }

        #endregion

        #region HMAC 加密

        /// 
        /// HmacSha1 加密
        /// 
        public static string HmacSha1(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACSHA1 hmacSha1 = new HMACSHA1(keyStr);
            return HashAlgorithmBase(hmacSha1, value, encoding);
        }

        /// 
        /// HmacSha256 加密
        /// 
        public static string HmacSha256(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACSHA256 hmacSha256 = new HMACSHA256(keyStr);
            return HashAlgorithmBase(hmacSha256, value, encoding);
        }

        /// 
        /// HmacSha384 加密
        /// 
        public static string HmacSha384(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACSHA384 hmacSha384 = new HMACSHA384(keyStr);
            return HashAlgorithmBase(hmacSha384, value, encoding);
        }

        /// 
        /// HmacSha512 加密
        /// 
        public static string HmacSha512(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACSHA512 hmacSha512 = new HMACSHA512(keyStr);
            return HashAlgorithmBase(hmacSha512, value, encoding);
        }

        /// 
        /// HmacMd5 加密
        /// 
        public static string HmacMd5(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACMD5 hmacMd5 = new HMACMD5(keyStr);
            return HashAlgorithmBase(hmacMd5, value, encoding);
        }

        /// 
        /// HmacRipeMd160 加密
        /// 
        public static string HmacRipeMd160(this string value, string keyVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }
            var encoding = Encoding.UTF8;
            byte[] keyStr = encoding.GetBytes(keyVal);
            HMACRIPEMD160 hmacRipeMd160 = new HMACRIPEMD160(keyStr);
            return HashAlgorithmBase(hmacRipeMd160, value, encoding);
        }

        #endregion

        #region AES 加密解密

        ///   
        /// AES加密  
        ///   
        /// 待加密字段  
        /// 密钥值  
        /// 加密辅助向量 
        ///   
        public static string AesStr(this string value, string keyVal, string ivVal)
        {
            if (value == null)
            {
                throw new ArgumentNullException("需要加密的字符串不能为空。");
            }

            var encoding = Encoding.UTF8;
            byte[] btKey = keyVal.FormatByte(encoding);
            byte[] btIv = ivVal.FormatByte(encoding);
            byte[] byteArray = encoding.GetBytes(value);
            string encrypt;
            Rijndael aes = Rijndael.Create();
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(btKey, btIv), CryptoStreamMode.Write))
                {
                    cStream.Write(byteArray, 0, byteArray.Length);
                    cStream.FlushFinalBlock();
                    encrypt = Convert.ToBase64String(mStream.ToArray());
                }
            }
            aes.Clear();
            return encrypt;
        }

        ///   
        /// AES解密  
        ///   
        /// 待加密字段  
        /// 密钥值  
        /// 加密辅助向量  
        ///   
        public static string UnAesStr(this string value, string keyVal, string ivVal)
        {
            var encoding = Encoding.UTF8;
            byte[] btKey = keyVal.FormatByte(encoding);
            byte[] btIv = ivVal.FormatByte(encoding);
            byte[] byteArray = Convert.FromBase64String(value);
            string decrypt;
            Rijndael aes = Rijndael.Create();
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(btKey, btIv), CryptoStreamMode.Write))
                {
                    cStream.Write(byteArray, 0, byteArray.Length);
                    cStream.FlushFinalBlock();
                    decrypt = encoding.GetString(mStream.ToArray());
                }
            }
            aes.Clear();
            return decrypt;
        }

        ///   
        /// AES Byte类型 加密  
        ///   
        /// 待加密明文  
        /// 密钥值  
        /// 加密辅助向量  
        ///   
        public static byte[] AesByte(this byte[] data, string keyVal, string ivVal)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(keyVal.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(ivVal.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] cryptograph;
            Rijndael aes = Rijndael.Create();
            try
            {
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
                    {
                        cStream.Write(data, 0, data.Length);
                        cStream.FlushFinalBlock();
                        cryptograph = mStream.ToArray();
                    }
                }
            }
            catch
            {
                cryptograph = null;
            }
            return cryptograph;
        }

        ///   
        /// AES Byte类型 解密  
        ///   
        /// 待解密明文  
        /// 密钥值  
        /// 加密辅助向量 
        ///   
        public static byte[] UnAesByte(this byte[] data, string keyVal, string ivVal)
        {
            byte[] bKey = new byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(keyVal.PadRight(bKey.Length)), bKey, bKey.Length);
            byte[] bVector = new byte[16];
            Array.Copy(Encoding.UTF8.GetBytes(ivVal.PadRight(bVector.Length)), bVector, bVector.Length);
            byte[] original;
            Rijndael aes = Rijndael.Create();
            try
            {
                using (MemoryStream mStream = new MemoryStream(data))
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
                    {
                        using (MemoryStream originalMemory = new MemoryStream())
                        {
                            byte[] buffer = new byte[1024];
                            int readBytes;
                            while ((readBytes = cStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                originalMemory.Write(buffer, 0, readBytes);
                            }

                            original = originalMemory.ToArray();
                        }
                    }
                }
            }
            catch
            {
                original = null;
            }
            return original;
        }

        #endregion

        #region DES 加密解密

        /// 
        /// DES 加密
        /// 
        public static string Des(this string value, string keyVal, string ivVal)
        {
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(value);
                var des = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(keyVal.Length > 8 ? keyVal.Substring(0, 8) : keyVal), IV = Encoding.ASCII.GetBytes(ivVal.Length > 8 ? ivVal.Substring(0, 8) : ivVal) };
                var desencrypt = des.CreateEncryptor();
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return BitConverter.ToString(result);
            }
            catch { return "转换出错!"; }
        }

        /// 
        /// DES 解密
        /// 
        public static string UnDes(this string value, string keyVal, string ivVal)
        {
            try
            {
                string[] sInput = value.Split("-".ToCharArray());
                byte[] data = new byte[sInput.Length];
                for (int i = 0; i < sInput.Length; i++)
                {
                    data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
                }
                var des = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(keyVal.Length > 8 ? keyVal.Substring(0, 8) : keyVal), IV = Encoding.ASCII.GetBytes(ivVal.Length > 8 ? ivVal.Substring(0, 8) : ivVal) };
                var desencrypt = des.CreateDecryptor();
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return Encoding.UTF8.GetString(result);
            }
            catch { return "解密出错!"; }
        }

        #endregion

        #region BASE64 加密解密

        /// 
        /// BASE64 加密
        /// 
        /// 待加密字段
        /// 
        public static string Base64(this string value)
        {
            var btArray = Encoding.UTF8.GetBytes(value);
            return Convert.ToBase64String(btArray, 0, btArray.Length);
        }

        /// 
        /// BASE64 解密
        /// 
        /// 待解密字段
        /// 
        public static string UnBase64(this string value)
        {
            var btArray = Convert.FromBase64String(value);
            return Encoding.UTF8.GetString(btArray);
        }

        #endregion

        #region Base64加密解密
        /// 
        /// Base64加密 可逆
        /// 
        /// 待加密文本
        /// 
        public static string Base64Encrypt(string value)
        {
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(value));
        }

        /// 
        /// Base64解密
        /// 
        /// 密文
        /// 
        public static string Base64Decrypt(string ciphervalue)
        {
            return System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(ciphervalue));
        }
        #endregion

        #region 内部方法

        /// 
        /// 转成数组
        /// 
        private static byte[] Str2Bytes(this string source)
        {
            source = source.Replace(" ", "");
            byte[] buffer = new byte[source.Length / 2];
            for (int i = 0; i < source.Length; i += 2) buffer[i / 2] = Convert.ToByte(source.Substring(i, 2), 16);
            return buffer;
        }

        /// 
        /// 转换成字符串
        /// 
        private static string Bytes2Str(this IEnumerable source, string formatStr = "{0:X2}")
        {
            StringBuilder pwd = new StringBuilder();
            foreach (byte btStr in source) { pwd.AppendFormat(formatStr, btStr); }
            return pwd.ToString();
        }

        private static byte[] FormatByte(this string strVal, Encoding encoding)
        {
            return encoding.GetBytes(strVal.Base64().Substring(0, 16).ToUpper());
        }

        /// 
        /// HashAlgorithm 加密统一方法
        /// 
        private static string HashAlgorithmBase(HashAlgorithm hashAlgorithmObj, string source, Encoding encoding)
        {
            byte[] btStr = encoding.GetBytes(source);
            byte[] hashStr = hashAlgorithmObj.ComputeHash(btStr);
            return hashStr.Bytes2Str();
        }

        #endregion

    }


  	  ViewData["sign"] = DdHelper.GetSign();
            ViewData["jsTicket"] = DdHelper.jsTicket;
            ViewData["nonce"] = DdHelper.nonce;
            ViewData["jsUrl"] = DdHelper.jsUrl;
            ViewData["timeStamp"] = DdHelper.timeStamp;


public String AutoLogin(string code) {
            return DdHelper.GetUserInfo(code);
        }




前台脚本

 var _config = [];
        _config.agentId = 131834322;
        _config.corpId = "";
        _config.timeStamp = '@ViewData["timeStamp"]';
        _config.nonceStr = "@ViewData["nonce"]";
        _config.signature = "@ViewData["sign"]";

        dd.config({
            agentId: _config.agentId,
            corpId: _config.corpId,
            timeStamp: _config.timeStamp,
            nonceStr: _config.nonceStr,
            signature: _config.signature,
            jsApiList: ['runtime.info', 'biz.contact.choose',
                     'device.notification.confirm', 'device.notification.alert',
                     'device.notification.prompt', 'biz.ding.post',
                     'biz.util.openLink']
        });

        dd.ready(function () {
            dd.runtime.info({
                onSuccess: function (info) {
                    logger.e('runtime info: ' + JSON.stringify(info));
                },
                onFail: function (err) {
                    logger.e('fail: ' + JSON.stringify(err));
                }
            });


            dd.runtime.permission.requestAuthCode({
                corpId: _config.corpId,
                onSuccess: function (result) {
                 YsDdAjax('/App/AutoLogin', { code: result.code }, function (data) {
                      dd.device.notification.alert({
                            message: JSON.stringify(data),
                            title: "提示",
                            buttonName: "个人信息",
                            onSuccess: function () {
                                //onSuccess将在点击button之后回调
                                /*回调*/
                            },
                            onFail: function (err) { }
                        });
                    });
                },
                onFail: function (err) { $('#userName').val(JSON.stringify(err)); }

            });
        });
        dd.error(function (err) {
            alert('dd error: ' + JSON.stringify(err));
        });



你可能感兴趣的:(C# 钉钉api接口开发(三) 免登录及自动刷新AccessToken)