示例#1
0
        /// <summary>
        /// 获取登录时设置的票据
        /// </summary>
        /// <param name="uid">用户标识</param>
        /// <param name="days">有效时间</param>
        /// <param name="roles">用户所属的角色</param>
        /// <param name="data">用户附加数据</param>
        /// <returns>认证票据</returns>
        public Dictionary <string, string> SignIn(string uid, int days, string[] roles, object data)
        {
            //实际上在这里 AuthTicket.UserId 和 FormsAuthenticationTicket里的 uid 冗余了
            //不过两者最好保持一致
            var expire   = DateTime.Now.AddDays(days);
            var dataJson = AuthTicket <object> .Create(uid, roles, data, expire).ToJson();

            var ticket      = new FormsAuthenticationTicket(2, uid, DateTime.Now, DateTime.Now.AddDays(days), true, dataJson);
            var cookieValue = FormsAuthentication.Encrypt(ticket);

            var cookieName = FormsAuthentication.FormsCookieName ?? typeof(FormsAuthAuthentication).Name;
            var cookiePath = FormsAuthentication.FormsCookiePath ?? "/";

            var setCookieValue = String.Format("{0}={1}; Path={2}; Domain={3}; HttpOnly",
                                               cookieName,
                                               cookieValue,
                                               cookiePath,
                                               FormsAuthentication.CookieDomain);
            var headers = new Dictionary <string, string>()
            {
                { "Set-Cookie", setCookieValue }
            };


            return(headers);
        }
示例#2
0
        /// <summary>
        /// 设置登录时设置的票据并返回
        /// </summary>
        /// <param name="uid">用户标识</param>
        /// <param name="roles">用户拥有的角色</param>
        /// <param name="days">有效时间</param>
        /// <param name="userData">用户附加数据(不需要可以设置成null)</param>
        /// <returns>认证票据</returns>
        public Dictionary <string, string> SignIn(string uid, int days, string[] roles, object userData)
        {
            //在这里, 将用户标识(Uid), 用户角色(Roles)和盐值拼接成字符串
            //解密的时候也要按照这个格式来拆分获取
            var expire    = DateTime.Now.AddDays(days);
            var plainText = AuthTicket <object> .Create(uid, roles, userData, expire).ToJson();

            var cipherBytes = SecurityUtils.AesEncrypt(plainText, AesKey);
            var cipherText  = Convert.ToBase64String(cipherBytes);

            var cookieValue = AuthUtils.CreateCookieValue(this.CookieName, cipherText, expire, "/");

            var headers = new Dictionary <string, string>
            {
                { "Set-Cookie", cookieValue }
            };

            return(headers);
        }