Skip to content

Latest commit

 

History

History
91 lines (80 loc) · 3.84 KB

File metadata and controls

91 lines (80 loc) · 3.84 KB

NHibernate Sidekick's ASP.NET Role Provider

This project is an implementation of the ASP.NET Role Provider using NHibernate and Sharp Architecture.

Changelog

  • 1.0.0
  • Basic methods have been implemented.

Implementation

1. Create your Role entity

Create the entity responsible for persisting data between the Role Provider and your database.

public class Role : NHibernate.Sidekick.Security.RoleProvider.Domain.RoleBase  { }

This model assumes your Role's Identifier is an integer. If this is not the case, inherit from RoleBaseWithTypedId<TId> instead.

2. Create your provider

This is who unobtrusively does all the work for you.

public class MembershipProvider : NHibernate.Sidekick.Security.MembershipProvider.Providers.MembershipProvider<User> { }

This model assumes your Role's Identifier is an integer. If this is not the case, inherit from MembershipProviderWithTypedId<T, TId> instead.

3. Ignore RoleBase from Fluent NHibernate's Automap generator

public class AutoPersistenceModelGenerator : SharpArch.NHibernate.FluentNHibernate.IAutoPersistenceModelGenerator
{
	public AutoPersistenceModel Generate()
    {
		AutoPersistenceModel mappings = AutoMap.AssemblyOf<User>(new AutomappingConfiguration());
		// Default Sharp Architecture options go here.		
		mappings.IgnoreBase<UserBase>();
		mappings.IgnoreBase<RoleBase>();
		mappings.IgnoreBase(typeof(UserBaseWithTypedId<>));
		mappings.IgnoreBase(typeof(RoleBaseWithTypedId<Role, User, int>));
	}
}

This step is only relevant if you're using Fluent NHibernate's Automapping mechanism.

4. Implement NHibernate Sidekick's Membership Provider

See NHibernate Sidekick's ASP.NET Membership Provider. This is necessary for it to be able to read and/or manipulate roles for a given User.

5. Set your provider's configuration options

Set this within your application's web.config:

<configuration>
	<system.web>
		<roleManager defaultProvider="SidekickRoleProvider" enabled="true" cacheRolesInCookie="true">
			<providers>
				<clear/>
				<add    name="SidekickRoleProvider" 
						applicationName="/Sidekick_Security_SampleApp" 
						type="NHibernate.Sidekick.Security.Sampler.Domain.RoleProvider" />
			</providers>
		</roleManager>
	</system.web>
</configuration>

Simple usage

@if (System.Web.Security.Roles.IsUserInRole("TestRole")) {
    <text>User has the given role.!</text>
}
[Authorize(Roles="TestRole")]
public class HomeController : Controller { }

If you're unfamiliar with ASP.NET's Role Provider, you can find more information on Microsoft's MSDN.

.NET Dependencies

  • .NET Framework 4.0
  • System
  • System.Core
  • System.Data
  • System.Configuration
  • System.Web
  • System.Web.ApplicationServices

Third-Party Dependencies

Contributors