欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學(xué)無止境
關(guān)注開優(yōu)網(wǎng)絡(luò) 關(guān)注前沿
ASP.NET公共類庫之類型轉(zhuǎn)換類TypeParse.cs
ASP.NET公共類庫之驗(yàn)證碼類ValidateImage.cs

ASP.NET公共類庫之用戶實(shí)用類User.cs

發(fā)表日期:2015-09-07    文章編輯:南昌開優(yōu)網(wǎng)絡(luò)    瀏覽次數(shù):3802    標(biāo)簽:ASP.NET應(yīng)用

/**********************************************
 * 類作用:   用戶實(shí)用類
 * 作者:開優(yōu)網(wǎng)絡(luò)
 * http://www.568387.com
 ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Svnhost.Common
{
    /// <summary>
    /// 用戶實(shí)用類,自定義窗體身份驗(yàn)證時(shí)可以使用。
    /// </summary>
    public sealed class UserUtil
    {
        /// <summary>
        /// 用戶登錄方法
        /// </summary>
        /// <param name="username">用戶名</param>
        /// <param name="roles">用戶角色</param>
        /// <param name="isPersistent">是否持久cookie</param>
        public static void Login(string username, string roles, bool isPersistent)
        {
            DateTime dt = isPersistent ? DateTime.Now.AddMinutes(99999) : DateTime.Now.AddMinutes(60);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                                                                                1, // 票據(jù)版本號
                                                                                username, // 票據(jù)持有者
                                                                                DateTime.Now, //分配票據(jù)的時(shí)間
                                                                                dt, // 失效時(shí)間
                                                                                isPersistent, // 需要用戶的 cookie 
                                                                                roles, // 用戶數(shù)據(jù),這里其實(shí)就是用戶的角色
                                                                                FormsAuthentication.FormsCookiePath);//cookie有效路徑

            //使用機(jī)器碼machine key加密cookie,為了安全傳送
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); //加密之后的cookie

            //將cookie的失效時(shí)間設(shè)置為和票據(jù)tikets的失效時(shí)間一致 
            HttpCookie u_cookie = new HttpCookie("username", username);
            if (ticket.IsPersistent)
            {
                u_cookie.Expires = ticket.Expiration;
                cookie.Expires = ticket.Expiration;
            }

            //添加cookie到頁面請求響應(yīng)中
            HttpContext.Current.Response.Cookies.Add(cookie);
            HttpContext.Current.Response.Cookies.Add(u_cookie);
        }

        /// <summary>
        /// 用戶退出方法
        /// </summary>
        public static void Logout()
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie == null)
            {
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            cookie.Expires = DateTime.Now.AddYears(-10);

            HttpCookie u_cookie = new HttpCookie("username", string.Empty);
            u_cookie.Expires = DateTime.Now.AddYears(-10);
            HttpContext.Current.Response.Cookies.Add(u_cookie);
        }
    }
}
主站蜘蛛池模板: 双峰县| 阳原县| 五台县| 茶陵县| 石城县| 大足县| 滨海县| 抚远县| 灯塔市| 贵南县| 石阡县| 南宁市| 读书| 德昌县| 扬中市| 永善县| 尼玛县| 寻乌县| 耒阳市| 惠安县| 剑河县| 临洮县| 东乡族自治县| 青铜峡市| 新余市| 罗源县| 土默特右旗| 武威市| 交城县| 甘孜县| 萨嘎县| 安宁市| 靖边县| 惠州市| 腾冲县| 嵊州市| 洛川县| 秭归县| 新建县| 来安县| 浦县|