我在使用nHibernate和Rhino.Security时遇到问题。经过数小时的努力来获取正确的配置设置后,我终于使代码可以正常运行。但是,除非我调用session.Flush(),否则不会将任何条目保存到数据库。
看网上的各种例子;我不应该打电话给同花顺。
这是我的配置代码:
var cfg = new Configuration()
.SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
.SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, "………")
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.UseSecondLevelCache, "true")
.SetProperty(Environment.UseQueryCache, "true")
.SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
.AddAssembly("GA.CAP.Website")
;
Security.Configure<AspNetUser>(cfg, SecurityTableStructure.Prefix);
var factory = cfg.BuildSessionFactory();
var session = factory.OpenSession();
var authorizationRepository = new AuthorizationRepository(session);
IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);
var permissionService = new PermissionsService(authorizationRepository, session);
IoC.Container.RegisterInstance<IPermissionsService>(permissionService);
var authService = new AuthorizationService(permissionService, authorizationRepository);
IoC.Container.RegisterInstance<IAuthorizationService>(authService);
测试代码:
authorizationRepository.CreateUsersGroup("GAAdmins");
var group = authorizationRepository.GetUsersGroupByName("GAAdmins");
GetUsersGroupByName调用返回null。如果我在两个调用之间添加一个session.Flush调用,它将正常工作并返回该组。
根据各种博客中的示例,例如this one,我不必调用flush。另外,Rhino.Security代码附带的测试用例不会进行任何刷新,如下所示:
这直接来自Rhino.Security的测试用例夹具:
// on first deploy
Operation operation = authorizationRepository.CreateOperation("/Account/View");
// when creating account
UsersGroup group = authorizationRepository.CreateUsersGroup("Belongs to " + account.Name);
// setting permission so only associated users can view
permissionsBuilderService
.Allow(operation)
.For(group)
.On(account)
.DefaultLevel()
.Save();
// when adding user to account
authorizationRepository.AssociateUserWith(user, group);
bool allowed = authorizationService.IsAllowed(user, account, "/Account/View");
Assert.True(allowed);
我在某处缺少某些设置吗?
谢谢,
里克
最佳答案
可以预期,RS使用与您的代码相同的会话,并且在内部调用Flush可能会导致意外的结果。
提交交易或致电Flush
关于c# - 除非我调用session.Flush(),否则NHIbernate&Rhino-Security不会保存。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3571196/