本文整理汇总了C#中PolicyContainer.EnforcePolicies方法的典型用法代码示例。如果您正苦于以下问题:C# PolicyContainer.EnforcePolicies方法的具体用法?C# PolicyContainer.EnforcePolicies怎么用?C# PolicyContainer.EnforcePolicies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PolicyContainer
的用法示例。
在下文中一共展示了PolicyContainer.EnforcePolicies方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Should_enforce_policies_with_context
public void Should_enforce_policies_with_context()
{
// Arrange
var roles = new List<object> { UserRole.Owner }.ToArray();
const bool isAuthenticated = true;
var context = new Mock<ISecurityContext>();
context.Setup(x => x.CurrenUserAuthenticated()).Returns(isAuthenticated);
context.Setup(x => x.CurrenUserRoles()).Returns(roles);
var policy = new Mock<ISecurityPolicy>();
policy.Setup(x => x.Enforce(It.Is<ISecurityContext>(c => c.CurrenUserAuthenticated() == isAuthenticated && c.CurrenUserRoles() == roles))).Returns(PolicyResult.CreateSuccessResult(policy.Object));
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(policy.Object);
// Act
policyContainer.EnforcePolicies(context.Object);
// Assert
policy.VerifyAll();
}
示例2: Should_return_the_same_results
public void Should_return_the_same_results()
{
// Arrange
var context = TestDataFactory.CreateSecurityContext(false);
context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.PerHttpSession;
var firstPolicy = new IgnorePolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(firstPolicy);
// Act
var results1 = policyContainer.EnforcePolicies(context);
var results2 = policyContainer.EnforcePolicies(context);
SecurityCache.ClearCache(Lifecycle.HybridHttpSession); ;
var results3 = policyContainer.EnforcePolicies(context);
var results4 = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
Assert.That(results3.Single(), Is.EqualTo(results4.Single()));
Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across sessions.");
}
示例3: Should_return_unique_results
public void Should_return_unique_results()
{
// Arrange
var context = TestDataFactory.CreateSecurityContext(false);
context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.DoNotCache;
var firstPolicy = new IgnorePolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(firstPolicy);
// Act
var results1 = policyContainer.EnforcePolicies(context);
var results2 = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
}
示例4: Should_use_cache_lifecycle_specified_when_adding_a_policy
public void Should_use_cache_lifecycle_specified_when_adding_a_policy()
{
// Arrange
const Cache defaultCacheLifecycle = Cache.PerHttpSession;
const Cache specifiedCacheLifecycle = Cache.PerHttpRequest;
var context = TestDataFactory.CreateSecurityContext(false);
context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = defaultCacheLifecycle;
var securityPolicy = new IgnorePolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(securityPolicy).Cache<IgnorePolicy>(specifiedCacheLifecycle);
// Act
var results1 = policyContainer.EnforcePolicies(context);
var results2 = policyContainer.EnforcePolicies(context);
SecurityCache.ClearCache(Lifecycle.HybridHttpContext); ;
var results3 = policyContainer.EnforcePolicies(context);
var results4 = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
Assert.That(results3.Single(), Is.EqualTo(results4.Single()));
Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
}
示例5: Should_stop_on_first_violation_and_return_2_results
public void Should_stop_on_first_violation_and_return_2_results()
{
// Arrange
var context = TestDataFactory.CreateSecurityContext(false);
var firstPolicy = new TestPolicy();
var secondPolicy = new Mock<ISecurityPolicy>();
secondPolicy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured"));
var thirdPolicy = new TestPolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy);
// Act
var results = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results.Count(), Is.EqualTo(2));
Assert.That(results.First().ViolationOccured, Is.False);
Assert.That(results.Last().ViolationOccured, Is.True);
}
示例6: Should_return_results
public void Should_return_results()
{
// Arrange
var roles = new List<object> { UserRole.Owner }.ToArray();
const bool isAuthenticated = true;
const string failureOccured = "Failure occured";
var context = TestDataFactory.CreateSecurityContext(isAuthenticated, roles);
var policy = new Mock<ISecurityPolicy>();
policy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(policy.Object, failureOccured));
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(policy.Object);
// Act
var results = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results.Count(), Is.EqualTo(1));
Assert.That(results.Single().ViolationOccured, Is.True);
Assert.That(results.Single().Message, Is.EqualTo(failureOccured));
}
示例7: Should_invoke_the_isautheticated_and_roles_functions
public void Should_invoke_the_isautheticated_and_roles_functions()
{
// Arrange
var context = MockRepository.GenerateMock<ISecurityContext>();
context.Expect(x => x.Runtime).Return(TestDataFactory.CreateSecurityRuntime()).Repeat.Once();
context.Expect(x => x.CurrentUserIsAuthenticated()).Return(true).Repeat.Once();
context.Expect(x => x.CurrentUserRoles()).Return(new List<object> { UserRole.Owner }.ToArray()).Repeat.Once();
context.Replay();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy(new TestPolicy());
// Act
policyContainer.EnforcePolicies(context);
// Assert
context.VerifyAllExpectations();
}
示例8: Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on
public void Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on()
{
// Arrange
var callsToContainer = 0;
var policy = new LazyLoadedPolicyWithCacheKey();
FakeIoC.GetAllInstancesProvider = () =>
{
callsToContainer++;
return new List<object> { policy };
};
SecurityConfigurator.Configure(configuration =>
{
configuration.GetAuthenticationStatusFrom(TestDataFactory.ValidIsAuthenticatedFunction);
configuration.ResolveServicesUsing(FakeIoC.GetAllInstances);
configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest);
});
var context = new MockSecurityContext(runtime: SecurityConfiguration.Current.Runtime);
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.AddPolicy<LazyLoadedPolicyWithCacheKey>();
// Act
policyContainer.EnforcePolicies(context);
policyContainer.EnforcePolicies(context);
// Assert
Assert.That(callsToContainer, Is.EqualTo(2));
Assert.That(policy.CacheKeyCallCount, Is.EqualTo(2), "Did not get the custom cache key the expected amount of times");
Assert.That(policy.EnforceCallCount, Is.EqualTo(1), "Did not call enforce the expected amount of times");
}
示例9: Should_return_the_same_results
public void Should_return_the_same_results()
{
// Arrange
var context = TestDataFactory.CreateSecurityContext(false);
var firstPolicy = new IgnorePolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest));
policyContainer.AddPolicy(firstPolicy);
// Act
var results1 = policyContainer.EnforcePolicies(context);
var results2 = policyContainer.EnforcePolicies(context);
SecurityCache.ClearCache(Lifecycle.HybridHttpContext);;
var results3 = policyContainer.EnforcePolicies(context);
var results4 = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
Assert.That(results3.Single(), Is.EqualTo(results4.Single()));
Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
}
示例10: Should_return_unique_results
public void Should_return_unique_results()
{
// Arrange
var firstPolicy = new IgnorePolicy();
var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.DoNotCache));
policyContainer.AddPolicy(firstPolicy);
// Act
var context = TestDataFactory.CreateSecurityContext(false);
var results1 = policyContainer.EnforcePolicies(context);
var results2 = policyContainer.EnforcePolicies(context);
// Assert
Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
}