Created
September 3, 2013 05:32
-
-
Save tiesont/6420026 to your computer and use it in GitHub Desktop.
Template implementation of a custom ASP.NET RoleProvider, using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using System.Configuration.Provider; | |
using System.Linq; | |
using System.Web.Configuration; | |
using System.Web.Security; | |
namespace Custom.Providers | |
{ | |
public class CustomRoleProvider : RoleProvider | |
{ | |
private string _applicationName; | |
private string _connectionString; | |
/// <summary> | |
/// Gets or sets the name of the application to store and retrieve role information for. | |
/// </summary> | |
/// <returns>The name of the application to store and retrieve role information for.</returns> | |
public override string ApplicationName | |
{ | |
get | |
{ | |
return _applicationName; | |
} | |
set | |
{ | |
_applicationName = value; | |
} | |
} | |
/// <summary> | |
/// [Not Implemented] Adds the specified user names to the specified roles for the configured applicationName. | |
/// </summary> | |
/// <param name="usernames">A string array of user names to be added to the specified roles.</param> | |
/// <param name="roleNames">A string array of the role names to add the specified user names to.</param> | |
/// <exception cref="NotImplementedException">This method has not been implemented, and will throw a <c>NotImplementedException</c> exception when called.</exception> | |
public override void AddUsersToRoles( string[] usernames, string[] roleNames ) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// [Not Implemented] Adds a new role to the data source for the configured applicationName. | |
/// </summary> | |
/// <param name="roleName">The name of the role to create.</param> | |
/// <exception cref="NotImplementedException">This method has not been implemented, and will throw a <c>NotImplementedException</c> exception when called.</exception> | |
public override void CreateRole( string roleName ) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// [Not Implemented] Removes a role from the data source for the configured applicationName. | |
/// </summary> | |
/// <param name="roleName">The name of the role to delete.</param> | |
/// <param name="throwOnPopulatedRole">If true, throw an exception if <paramref name="roleName"/> has one or more members and do not delete <paramref name="roleName"/>.</param> | |
/// <returns> | |
/// true if the role was successfully deleted; otherwise, false. | |
/// </returns> | |
/// <exception cref="NotImplementedException">This method has not been implemented, and will throw a <c>NotImplementedException</c> exception when called.</exception> | |
public override bool DeleteRole( string roleName, bool throwOnPopulatedRole ) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// [Not Implemented] Gets an array of user names in a role where the user name contains the specified user name to match. | |
/// </summary> | |
/// <param name="roleName">The role to search in.</param> | |
/// <param name="usernameToMatch">The user name to search for.</param> | |
/// <returns> | |
/// A string array containing the names of all the users where the user name matches <paramref name="usernameToMatch"/> and the user is a member of the specified role. | |
/// </returns> | |
/// <exception cref="NotImplementedException">This method has not been implemented, and will throw a <c>NotImplementedException</c> exception when called.</exception> | |
public override string[] FindUsersInRole( string roleName, string usernameToMatch ) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Gets a list of all the roles for the configured applicationName. | |
/// </summary> | |
/// <returns> | |
/// A string array containing the names of all the roles stored in the data source for the configured applicationName. | |
/// </returns> | |
public override string[] GetAllRoles() | |
{ | |
throw new NotImplementedException(); // NOTE: this needs to be implemented | |
} | |
/// <summary> | |
/// Gets a list of the roles that a specified user is in for the configured applicationName. | |
/// </summary> | |
/// <param name="username">The user to return a list of roles for.</param> | |
/// <returns> | |
/// A string array containing the names of all the roles that the specified user is in for the configured applicationName. | |
/// </returns> | |
public override string[] GetRolesForUser( string username ) | |
{ | |
throw new NotImplementedException(); // NOTE: this needs to be implemented | |
} | |
/// <summary> | |
/// Gets a list of users in the specified role for the configured applicationName. | |
/// </summary> | |
/// <param name="roleName">The name of the role to get the list of users for.</param> | |
/// <returns> | |
/// A string array containing the names of all the users who are members of the specified role for the configured applicationName. | |
/// </returns> | |
public override string[] GetUsersInRole( string roleName ) | |
{ | |
throw new NotImplementedException(); // NOTE: this needs to be implemented | |
} | |
/// <summary> | |
/// Gets a value indicating whether the specified user is in the specified role for the configured applicationName. | |
/// </summary> | |
/// <param name="username">The user name to search for.</param> | |
/// <param name="roleName">The role to search in.</param> | |
/// <returns> | |
/// true if the specified user is in the specified role for the configured applicationName; otherwise, false. | |
/// </returns> | |
public override bool IsUserInRole( string username, string roleName ) | |
{ | |
throw new NotImplementedException(); // NOTE: this needs to be implemented | |
} | |
/// <summary> | |
/// [Not Implemented] Removes the specified user names from the specified roles for the configured applicationName. | |
/// </summary> | |
/// <param name="usernames">A string array of user names to be removed from the specified roles.</param> | |
/// <param name="roleNames">A string array of role names to remove the specified user names from.</param> | |
/// <exception cref="NotImplementedException">This method has not been implemented, and will throw a <c>NotImplementedException</c> exception when called.</exception> | |
public override void RemoveUsersFromRoles( string[] usernames, string[] roleNames ) | |
{ | |
throw new NotImplementedException(); | |
} | |
/// <summary> | |
/// Gets a value indicating whether the specified role name already exists in the role data source for the configured applicationName. | |
/// </summary> | |
/// <param name="roleName">The name of the role to search for in the data source.</param> | |
/// <returns> | |
/// true if the role name already exists in the data source for the configured applicationName; otherwise, false. | |
/// </returns> | |
public override bool RoleExists( string roleName ) | |
{ | |
throw new NotImplementedException(); // NOTE: this needs to be implemented | |
} | |
#region Initialization | |
/// <summary> | |
/// Initializes the provider. | |
/// </summary> | |
/// <param name="name">The friendly name of the provider.</param> | |
/// <param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param> | |
/// <exception cref="T:System.ArgumentNullException">The name of the provider is null.</exception> | |
/// | |
/// <exception cref="T:System.ArgumentException">The name of the provider has a length of zero.</exception> | |
/// | |
/// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception> | |
public override void Initialize(string name, NameValueCollection config) | |
{ | |
if (config == null) | |
{ | |
throw new ArgumentNullException("config"); | |
} | |
if (name == null || name.Length == 0) | |
{ | |
name = "CustomRoleProvider"; | |
} | |
if (String.IsNullOrEmpty(config["description"])) | |
{ | |
config.Remove("description"); | |
config.Add("description", "Custom RoleProvider"); | |
} | |
// Initialize the abstract base class. | |
base.Initialize(name, config); | |
_applicationName = GetConfigValue(config, "applicationName", System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); | |
var ConnectionStringSettings = WebConfigurationManager.ConnectionStrings[config["connectionStringName"]]; | |
if ((ConnectionStringSettings == null) || (string.IsNullOrWhiteSpace(ConnectionStringSettings.ConnectionString.Trim()))) | |
{ | |
throw new ProviderException("Connection string cannot be blank."); | |
} | |
_connectionString = ConnectionStringSettings.ConnectionString; | |
} | |
/// <summary> | |
/// Gets the configuration value. | |
/// </summary> | |
/// <param name="configValue">The configuration <c>NameValueCollection</c>.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <returns></returns> | |
private string GetConfigValue(NameValueCollection configuration, string key, string defaultValue) | |
{ | |
if (configuration == null || String.IsNullOrEmpty(key)) | |
{ | |
return defaultValue; | |
} | |
return configuration[key]; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment