Пример #1
0
        public void AuthorizeAttributeReturnsUniqueTypeIDs() {
            // Arrange
            AuthorizeAttribute attr1 = new AuthorizeAttribute();
            AuthorizeAttribute attr2 = new AuthorizeAttribute();

            // Assert
            Assert.AreNotEqual(attr1.TypeId, attr2.TypeId);
        }
Пример #2
0
        private void ReadSettingsFromBindAttribute()
        {
            BindAttribute attr = (BindAttribute)Attribute.GetCustomAttribute(_parameterInfo, typeof(BindAttribute));

            if (attr == null)
            {
                return;
            }

            _exclude = new ReadOnlyCollection <string>(AuthorizeAttribute.SplitString(attr.Exclude));
            _include = new ReadOnlyCollection <string>(AuthorizeAttribute.SplitString(attr.Include));
            _prefix  = attr.Prefix;
        }
        public void Before_each_test()
        {
            attribute = new SessionBasedAuthorizeAttibute();

            var controller = new FakeController();
            MvcTest.SetupControllerContext(controller);

            authorizationContext = new AuthorizationContext();
            authorizationContext.Controller = controller;
            authorizationContext.HttpContext = controller.HttpContext;

            var controllerDescriptor = new ReflectedControllerDescriptor(typeof(FakeController));
            var method = typeof(FakeController).GetMethod("Nothing");
            authorizationContext.ActionDescriptor = new ReflectedActionDescriptor(method, "Nothing", controllerDescriptor);
        }
Пример #4
0
        public void UsersProperty() {
            // Arrange
            AuthorizeAttribute attr = new AuthorizeAttribute();

            // Act & assert
            MemberHelper.TestStringProperty(attr, "Users", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
Пример #5
0
        public void OnAuthorizationThrowsIfFilterContextIsNull() {
            // Arrange
            AuthorizeAttribute attr = new AuthorizeAttribute();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    attr.OnAuthorization(null);
                }, "filterContext");
        }
        public void UsersProperty() {
            // Arrange
            AuthorizeAttribute attr = new AuthorizeAttribute();

            // Act & assert
            MemberHelper.TestStringProperty(attr, "Users", String.Empty);
        }
Пример #7
0
 public OpenAuthorizeAttribute(AuthorizeAttribute attribute)
 {
     Order = attribute.Order;
     Roles = attribute.Roles;
     Users = attribute.Users;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InternalAuthorize"/> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 public InternalAuthorize(AuthorizeAttribute attribute)
 {
     Order = attribute.Order;
     Roles = attribute.Roles;
     Users = attribute.Users;
 }
 protected virtual bool VerifyAuthorizeAttribute(AuthorizeAttribute authorizeAttribute, ControllerContext controllerContext, ActionDescriptor actionDescriptor)
 {
     var authorizationContext = this.mvcContextFactory.CreateAuthorizationContext(controllerContext, actionDescriptor);
     authorizeAttribute.OnAuthorization(authorizationContext);
     if (authorizationContext.Result != null)
         return false;
     return true;
 }
        protected virtual bool VerifyAuthorizeAttribute(AuthorizeAttribute authorizeAttribute, ControllerContext controllerContext)
        {
            // Reasoning for using Reflection and AuthorizeAttribute rather than IAuthorizationFilter
            // http://weblogs.asp.net/rashid/archive/2009/09/06/asp-net-mvc-and-authorization-and-monkey-patching.aspx

            var currentAuthorizationAttributeType = authorizeAttribute.GetType();
            var isDefaultAttribute = (currentAuthorizationAttributeType == defaultAuthorizeAttributeType);

            var subclassedAttribute =
                isDefaultAttribute ?
                new InternalAuthorizeAttribute() : // No need to use Reflection.Emit when ASP.NET MVC built-in attribute is used
                authorizeAttribute is IAuthorizeAttribute ?
                authorizeAttribute as IAuthorizeAttribute :
                authorizeAttributeBuilder.Build(currentAuthorizationAttributeType).Invoke(new object[0]) as IAuthorizeAttribute;

            subclassedAttribute.Order = authorizeAttribute.Order;
            subclassedAttribute.Roles = authorizeAttribute.Roles;
            subclassedAttribute.Users = authorizeAttribute.Users;

            if (!isDefaultAttribute)
            {
                // Copy remaining properties
                objectCopier.Copy(authorizeAttribute, subclassedAttribute, "Order", "Roles", "Users");
            }

            return subclassedAttribute.IsAuthorized(controllerContext.HttpContext);
        }