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

📄 user.cs

📁 这个是网上相册系统的一个架构设计说明
💻 CS
字号:
using System;
using System.IO;
using System.Security.Principal;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Data;
using System.Data.SqlClient;

namespace Utils
{
	/// <summary>
	/// User 的摘要说明。
	/// </summary>
	public sealed class User
	{
		private User()
		{
		}

		static public string CheckUser(string userName, string pwd)
		{
			using  (SqlConnection cn  =   new  SqlConnection(AlbumConfig.DBConnectionString))
			{
				cn.Open();
				SqlCommand cmd  =   new  SqlCommand("CheckUser", cn);
				cmd.CommandType = CommandType.StoredProcedure;
				SqlParameter paraUsrName = new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar, 50);
				paraUsrName.Value = userName;
				cmd.Parameters.Add(paraUsrName);
				SqlParameter paraPwd = new System.Data.SqlClient.SqlParameter("@Pwd", System.Data.SqlDbType.VarChar, 10);
				paraPwd.Value = pwd;
				cmd.Parameters.Add(paraPwd);
				SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
				if(dr.Read())
				{
					int usrId = (int)dr["UserID"];
					string name = dr["Alias"].ToString();
			
					MemoryStream strm = new MemoryStream();
					GenericIdentity id = new GenericIdentity(userName);
					IPrincipal pricipal = new MyPrincipal(usrId, name, id, new string[]{"User"});
			
					try 
					{
						BinaryFormatter formatter = new BinaryFormatter();
						formatter.Serialize(strm, pricipal);
						int len = (int)strm.Length;
						byte[] data = new byte[len];
						strm.Seek(0, SeekOrigin.Begin);
						strm.Read(data, 0, len);
						return Convert.ToBase64String(data);
					}
					catch (SerializationException) 
					{
						throw;
					}
					finally 
					{
						strm.Close();
					}
				}
			}
			return null;
		}

		static public IPrincipal CheckUser(string bas64string)
		{
			byte[] bytes = Convert.FromBase64String(bas64string);
			MemoryStream strm = new MemoryStream(bytes);
			
			BinaryFormatter formatter = new BinaryFormatter();
			try 
			{
				MyPrincipal pricipal = formatter.Deserialize(strm) as MyPrincipal;
				return pricipal;
			}
			catch (SerializationException) 
			{
				throw;
			}
			finally 
			{
				strm.Close();
			}
		}
	}

	[Serializable]
	public class MyPrincipal : GenericPrincipal 
	{
		public MyPrincipal(int usrId, string name,IIdentity identity,string[] roles)
			: base(identity, roles)
		{
			_usrId = usrId;
			_name = name;
		}

		public int UserID
		{
			get{return _usrId;}
		}

		public string Name
		{
			get{return _name;}
		}

		protected int _usrId;
		protected string _name;
	}
}

⌨️ 快捷键说明

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