⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 st_identity.cs

📁 学校的考试系统
💻 CS
字号:
using System;
using System.Security.Principal;
using System.Collections;
using System.Globalization;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace STGROUP.ST_ExamBiz
{
	/// <summary>
	/// ST_Identity 的摘要说明。
	/// </summary>
	public class ST_Identity : IIdentity
	{
		protected string _name;
		protected int _userID=0;
		private string _Roles = "";

		public ST_Identity(string username)
		{
			HttpCookie cookie = HttpContext.Current.Request.Cookies["ST_Rights"];
			if (cookie != null)
			{
				string v = FormsAuthentication.Decrypt(cookie.Value).UserData;
				if (!(v == null || v.Trim() == String.Empty))
				{				
					string[] values = v.Split('|');
					this._Roles = values[0];
					this._name = values[1];
					this._userID = int.Parse(values[2]);
				}
			}
		}

		public ST_Identity(string name,int userID)
		{
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}

			_name=name;
			_userID=userID;
		}

		public void Save()
		{
			string values = String.Empty;
			values += this.Roles + "|";

			values += this.Name + "|";
			values += this.UserID + "|";
			FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
				1,
				Name,
				DateTime.Now,
				DateTime.Now.AddMinutes(60),
				false,
				values,
				"/"
				);
			HttpContext.Current.Response.Cookies.Add(new HttpCookie("ST_Rights", FormsAuthentication.Encrypt(ticket)));

		}

		#region IIdentity 成员

		/// <summary>
		/// 只读属性,返回用户是否通过了验证
		/// </summary>
		public bool IsAuthenticated
		{
			get
			{
				return !this._name.Equals("");
			}
		}
		/// <summary>
		/// 只读属性,返回身份验证类型
		/// </summary>
		public string AuthenticationType
		{
			get
			{
				return "form";
			}
		}
		/// <summary>
		/// 只读属性,返回用户名
		/// </summary>
		public string Name
		{
			get
			{
				return _name;
			}
			set
			{
				_name = value;
			}
		}
		
		/// <summary>
		/// 只读属性,返回User表中userID属性
		/// </summary>
		public int UserID
		{
			get{return _userID;}
		}

		/// <summary>
		/// 用户当前的角色,只有一个角色。
		/// </summary>
		public string Roles 
		{
			get { return _Roles;}
			set { _Roles = value;}
		}



		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -