zoukankan      html  css  js  c++  java
  • 自动登录或7天登录的实现

    后台cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Management;
    
    namespace WebApplication2
    {
        public partial class WebForm3 : System.Web.UI.Page
        {
            /// <summary>
            /// pageLoad事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Page_Load(object sender, EventArgs e)
            {
                //自动登录
                if (!IsPostBack)
                {
                   if (!String.IsNullOrEmpty(CookieUtil.ReadCookies("eBooking_SGUserInfo")))
                    { //自动登录
                        UserLogin("", "");
                    }
                   else
                   {
                       //普通登录
                   }
                }
            }
    
            /// <summary>
            /// 登录方法
            /// </summary>
            /// <param name="strLoginName"></param>
            /// <param name="strPASSWORD"></param>
            private void UserLogin(string strLoginName, string strPASSWORD) 
            {
                UserInfoResponse userEntity = null;//创建的实体
                if (!String.IsNullOrEmpty(CookieUtil.ReadCookies("eBooking_SGUserInfo")))
                {
                    string decryCookie = DESEncrypt.DecryptDES(CookieUtil.ReadCookies("eBooking_SGUserInfo"),ConfigurationManager.AppSettings["DesKey"].ToString());
                    string[] cookieValue = decryCookie.Split('|');
                    if (cookieValue.Length > 0)
                    {
                        if (cookieValue[1] == GetNetCardMacAddress())
                        {
                            userEntity = null;//这里根据cookieValue[0](用户名)去数据库查询用户相关信息
                        }
                    }
                }
                else
                {
                    userEntity = null;//这里是普通登录 
                }
    
    
                if (true)//这里判断自动登录按钮是否选中
                {
                    string cookieValue = GetUserLoginCookieValue();//得到cookie
                    //写cookie
                    CookieUtil.WriteCookies("eBooking_SGUserInfo", cookieValue, 7,CookieUtil.TimeUnit.Day);
                }
            }
    
    
            #region 得到用户Cookie Value
            /// <summary>
            /// 得到用户Cookie value
            /// </summary>
            /// <returns></returns>
            private string GetUserLoginCookieValue()
            {
                string cookieValue = "";
                string UserCode = "";//用户名的Code
    
                cookieValue =UserCode + "|" + GetNetCardMacAddress();//用户名和mac地址
                //加密
                string EncryCookieValue = DESEncrypt.EncryptDES(cookieValue, ConfigurationManager.AppSettings["DesKey"]);
                return EncryCookieValue;
            }
            #endregion
    
            #region 获取MAC地址
            /// <summary>
            /// 获取MAC地址
            /// </summary>
            /// <returns></returns>
            public string GetNetCardMacAddress()
            {
    
                ManagementClass mc;//注意引用System.Management和System.Management.Instrumentation
                ManagementObjectCollection moc;
                mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                moc = mc.GetInstances();
                string str = "";
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        str = mo["MacAddress"].ToString();
                }
                return str;
            }
            #endregion
        }
    }
    

     要用到的两个类

    CookieUtil.cs

    cookie类 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication2
    {
        public class CookieUtil
        {
            /// <summary>
            /// 写Cookies
            /// </summary>
            /// <param name="indexValue">Cookie名</param>
            /// <param name="textValue">Cookie值</param>
            /// <param name="expries">过期时间</param>
            /// <param name="times">时间单位枚举</param>
            public static void WriteCookies(string indexValue, string textValue, int expries, TimeUnit times)
            {
                if (HttpContext.Current.Response.Cookies[indexValue] != null)
                    HttpContext.Current.Response.Cookies[indexValue].Expires = DateTime.Now.AddDays(-1);
    
                HttpContext.Current.Response.Cookies[indexValue].Value = textValue;
    
                DateTime dtNow = DateTime.Now;
    
                switch (times)
                {
                    case TimeUnit.Second:
                        dtNow = dtNow.AddSeconds(expries);
                        break;
                    case TimeUnit.Minute:
                        dtNow = dtNow.AddMinutes(expries);
                        break;
                    case TimeUnit.Hour:
                        dtNow = dtNow.AddHours(expries);
                        break;
                    case TimeUnit.Day:
                        dtNow = dtNow.AddDays(expries);
                        break;
                    case TimeUnit.Month:
                        dtNow = dtNow.AddMonths(expries);
                        break;
                    default:
                        dtNow = dtNow.AddYears(expries);
                        break;
                }
    
                HttpContext.Current.Response.Cookies[indexValue].Expires = dtNow;
            }
    
            /// <summary>
            /// 读Cookies
            /// </summary>
            /// <param name="indexValue">Cookie名</param>
            /// <returns>返回Cookie 值</returns>
            public static string ReadCookies(string indexValue)
            {
                if (HttpContext.Current.Request.Cookies[indexValue] != null)
                {
                    //Encoding stre = Encoding.GetEncoding("GB2312");
                    return HttpContext.Current.Request.Cookies[indexValue].Value.ToString();
                }
    
                return string.Empty;
            }
    
            /// <summary>
            /// 删除Cookies,立即过期
            /// </summary>
            /// <param name="indexValue">Cookie名</param>
            /// <param name="times">时间单位枚举</param>
            public static void DeleteCookies(string indexValue, TimeUnit times)
            {
                if (HttpContext.Current.Request.Cookies[indexValue] != null)
                {
                    HttpCookie cookies = HttpContext.Current.Request.Cookies[indexValue];
    
                    DateTime dtNow = DateTime.Now;
    
                    switch (times)
                    {
                        case TimeUnit.Second:
                            dtNow = dtNow.AddSeconds(-1);
                            break;
                        case TimeUnit.Minute:
                            dtNow = dtNow.AddMinutes(-1);
                            break;
                        case TimeUnit.Hour:
                            dtNow = dtNow.AddHours(-1);
                            break;
                        case TimeUnit.Day:
                            dtNow = dtNow.AddDays(-1);
                            break;
                        case TimeUnit.Month:
                            dtNow = dtNow.AddMonths(-1);
                            break;
                        default:
                            dtNow = dtNow.AddYears(-1);
                            break;
                    }
    
                    cookies.Expires = dtNow;
                    HttpContext.Current.Response.Cookies.Add(cookies);
                }
            }
    
            public enum TimeUnit
            {
                /// <summary>
                /// 秒
                /// </summary>
                Second,
                /// <summary>
                /// 分
                /// </summary>
                Minute,
                /// <summary>
                /// 时
                /// </summary>
                Hour,
                /// <summary>
                /// 天
                /// </summary>
                Day,
                /// <summary>
                /// 月
                /// </summary>
                Month,
                /// <summary>
                /// 年
                /// </summary>
                Year
            }
        }
    }
    

    加密类

      DESEncrypt.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    
    namespace WebApplication2
    {
        public class DESEncrypt
        {
            /// <summary>
            /// 默认密钥向量
            /// </summary>
            private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    
            /// <summary>
            /// DES加密字符串
            /// </summary>
            /// <param name="encryptString">待加密的字符串</param>
            /// <param name="encryptKey">加密密钥,要求为8位</param>
            /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
            public static String EncryptDES(string encryptString, string encryptKey)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] bIV = IV;
                byte[] bStr = Encoding.UTF8.GetBytes(encryptString);
                try
                {
                    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                    MemoryStream mStream = new MemoryStream();
                    CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
                    cStream.Write(bStr, 0, bStr.Length);
                    cStream.FlushFinalBlock();
                    return Convert.ToBase64String(mStream.ToArray());
                }
                catch
                {
                    return string.Empty;
                }
            }
    
            /// <summary>
            /// DES解密字符串
            /// </summary>
            /// <param name="decryptString">待解密的字符串</param>
            /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同></param>
            /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
            public static String DecryptDES(String decryptString, String decryptKey)
            {
                try
                {
                    byte[] bKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                    byte[] bIV = IV;
                    byte[] bStr = Convert.FromBase64String(decryptString);
                    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                    MemoryStream mStream = new MemoryStream();
                    CryptoStream cStream = new CryptoStream(mStream, desc.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
                    cStream.Write(bStr, 0, bStr.Length);
                    cStream.FlushFinalBlock();
                    return Encoding.UTF8.GetString(mStream.ToArray());
                }
                catch
                {
                    return string.Empty;
                }
            }
        }
    }
    

      ok

  • 相关阅读:
    shell 脚本编程学习
    LMH6505 vs AD8336
    ubuntu 8.04 NFS服务的配置(转)
    新博客开张了!
    基于ARMlinux环境下的音频系统开发
    可恶的英语考试
    转HashTable(C#)
    我的手机3300
    高效注册DLL控件 让你的IE浏览器复活
    学习.NET2.0随笔
  • 原文地址:https://www.cnblogs.com/xiyangmeng/p/2816555.html
Copyright © 2011-2022 走看看