Exemplo n.º 1
0
 /// <summary>
 /// Set the metadata of authentication.
 /// </summary>
 /// <param name="authentication"></param>
 protected virtual void SetAuthentication(PropertyAuthenticationAttribute authentication)
 {
     if (authentication == null)
     {
         throw new ArgumentNullException("authentication");
     }
     AllowAnonymous             = authentication.AllowAnonymous;
     EditRoles                  = new ReadOnlyCollection <object>(authentication.EditRolesRequired);
     ViewRoles                  = new ReadOnlyCollection <object>(authentication.ViewRolesRequired);
     AuthenticationRequiredMode = authentication.Mode;
 }
        public override byte[] Serialize(object obj)
        {
            Type type = obj.GetType();

            if (!MainEntity && typeof(EntityBase).IsAssignableFrom(type))
            {
                return(((EntityBase)obj).Index.ToByteArray());
            }

            MainEntity = false;

            BinaryDataWriter writer = new BinaryDataWriter();

            var properties = type.GetProperties();

            for (byte i = 0; i < properties.Length; i++)
            {
                var property = properties[i];

                #region 判断是否有效数据

                if (!property.CanRead || !property.CanWrite)
                {
                    continue;
                }
                PropertyAuthenticationAttribute authenticate = property.GetCustomAttributes(typeof(PropertyAuthenticationAttribute), false).FirstOrDefault() as PropertyAuthenticationAttribute;
                if (authenticate != null)
                {
                    if (!authenticate.AllowAnonymous && !MemberManager.IsSigned)
                    {
                        continue;
                    }
                    if (!RoleManager.HasRoles(authenticate.ViewRolesRequired))
                    {
                        continue;
                    }
                }

                #endregion

                writer.WriteByte(i);

                var data = Serialize(property.GetValue(obj, null));
                writer.WriteBytes(data);
            }

            MainEntity = true;
            return(writer.ToArray());
        }