Click here to Skip to main content
15,887,135 members
Articles / Web Development / ASP.NET

Custom Authentication provider by implementing IHttpModule, IPrincipal and IIdentity

Rate me:
Please Sign up or sign in to vote.
4.55/5 (41 votes)
2 Nov 20033 min read 359.1K   10.5K   159  
An article on writing Custom Authentication provider in ASP.NET
using System;
using System.Security.Principal;

namespace CustomSecurity
{
	/// <summary>
	/// Represents the Identity of a User. 
	/// Stores the details of a User. 
	/// Implements the IIDentity interface.
	/// </summary>
	[Serializable]
	public class CustomIdentity : IIdentity
	{
		# region private variables
		
		private String userId;
		private int userPK;
		private bool login;
		private bool isSuperUser;
		private string fullName;
		private string userEmail;
		private string roles;

		#endregion

		# region constructors
		/// <summary>
		/// The default constructor initializes any fields to their default values.
		/// </summary>
		public CustomIdentity()
		{
			this.userPK			= 0;
			this.userId			= String.Empty;
			this.login			= false;
			this.isSuperUser	= false;
			this.fullName		= String.Empty;
			this.userEmail		= String.Empty;
			this.roles			= String.Empty;
		}

		/// <summary>
		/// Initializes a new instance of the CustomIdentity class 
		/// with the passed parameters
		/// </summary>
		/// <param name="uId">User ID of the user</param>
		/// <param name="upk">Primary Key of the User record in User table</param>
		/// <param name="islogin">Flag that indicates whether the user has been authenticated</param>
		/// <param name="isAdmin">Flag that indicates whether the user is an Administrator</param>
		/// <param name="userName">Full name of the User</param>
		/// <param name="email">Email of the User</param>
		public CustomIdentity(string uId, int upk, bool islogin, bool isAdmin, string userName, string email, string uRoles)
		{
			this.userPK			= upk;
			this.userId			= uId;
			this.login			= islogin;
			this.isSuperUser	= isAdmin;
			this.fullName		= userName;
			this.userEmail		= email;
			this.roles			= uRoles;
		}

		#endregion

		# region properties
		// Properties
		/// <summary>
		/// Gets the Authentication Type
		/// </summary>
		public string AuthenticationType 
		{
			get { return "Custom"; }
		}

		/// <summary>
		/// Indicates whether the User is authenticated
		/// </summary>
		public bool IsAuthenticated  
		{
			get { return login; }
			set { login = value; }
		}

		/// <summary>
		/// Gets or sets the UserID of the User
		/// </summary>
		public string Name 
		{
			get { return userId; }
			set { userId = value; }
		}

		/// <summary>
		/// Gets or sets the Primary Key for the User record
		/// </summary>
		public int UserPK 
		{
			get { return userPK; }
			set { userPK = value; }
		}

		/// <summary>
		/// Indicates whether the User is an Administrator
		/// </summary>
		public bool IsSuperUser
		{
			get { return isSuperUser; }
			set { isSuperUser = value; }
		}

		/// <summary>
		/// Gets or sets the Full Name of the User
		/// </summary>
		public string UserFullName
		{
			get { return fullName; }
			set { fullName = value; }
		}

		/// <summary>
		/// Gets or sets the Email of the User
		/// </summary>
		public string UserEmail
		{
			get { return userEmail; }
			set { userEmail = value; }
		}

		/// <summary>
		/// Gets or sets the Roles of the User
		/// The roles are stored in a pipe "|" separated string
		/// </summary>
		public string UserRoles
		{
			get { return roles; }
			set { roles = value; }
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions