siteprincipal.cs

来自「基于角色的用户验证. 这个程序重点写了四个pages以实现这些功能:增加用户」· CS 代码 · 共 69 行

CS
69
字号
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace RolesBasedAthentication
{
	public class SitePrincipal: System.Security.Principal.IPrincipal
	{
		protected System.Security.Principal.IIdentity identity;
		protected ArrayList roleList;

		public SitePrincipal( string email )
		{		
			identity = new SiteIdentity(email);
			roleList = User.GetUserRoles( ((SiteIdentity)identity).UserID );
		}

		public SitePrincipal( int userID )
		{	
			identity = new SiteIdentity(userID);
			roleList = User.GetUserRoles( ((SiteIdentity)identity).UserID );
		}

		public static SitePrincipal ValidateLogin(string email, string password)
		{
			int userID;
			string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
			SqlConnection connection = new SqlConnection(connectionString);

			connection.Open();

			SqlCommand command = new SqlCommand("ValidateLogin", connection);
			command.Parameters.Add("@Email", email);
			command.Parameters.Add("@Password", password);
			// add return value parameter
			command.Parameters.Add( new SqlParameter ( "ReturnValue",SqlDbType.Int,4,
				ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null ));
			command.CommandType = CommandType.StoredProcedure;

			command.ExecuteNonQuery();
			userID = (int)command.Parameters["ReturnValue"].Value;

			command.Dispose();
			connection.Close();

			return (userID == 0 ? null : new SitePrincipal(userID));
		}

		public bool IsInRole(string role)
		{
			return roleList.Contains( role );
		}

		// Properties
		public System.Security.Principal.IIdentity Identity
		{
			get { return identity; }
			set { identity = value; }
		}

		public ArrayList Roles
		{
			get { return roleList; }
		}
	}
}

⌨️ 快捷键说明

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