コード例 #1
0
        //TODO change JWT
        public static List <string> CheckJwt(this object obj, ClaimsPrincipal user)
        {
            Type type = obj.GetType();

            try
            {
                List <string> errorList = new List <string>();
                foreach (var field in type.GetProperties())
                {
                    var attribute = field.GetCustomAttribute <PropsAttribute>();
                    if (attribute == null)
                    {
                        attribute = new PropsAttribute(name: field.Name);
                    }
                    #region SetProperty
                    if (!string.IsNullOrEmpty(attribute.JWTKey))
                    {
                        var item = user.FindFirst(attribute.JWTKey).Value;
                        if (!string.IsNullOrEmpty(item))
                        {
                            field.SetValue(obj, Convert.ChangeType(item, field.PropertyType));
                        }
                    }
                    #endregion
                    var isRequired = field.GetCustomAttribute <RequiredAttribute>();
                    if (isRequired != null || attribute.Required)
                    {
                        var value = field.GetValue(type);
                        if (value == null)
                        {
                            errorList.Add(attribute.Label ?? field.Name + "is null");
                        }
                    }
                }
                return(errorList);
            }
            catch (Exception ext)
            {
                throw new Exception(ext.Message, ext);
            }
        }
コード例 #2
0
        public static PropsAttribute GetProps(this System.Reflection.PropertyInfo i)
        {
            var attribute = i.GetCustomAttribute <PropsAttribute>();

            if (attribute == null && i.GetGetMethod().IsVirtual)
            {
                return(null);
            }
            var            foregnKey    = i.GetCustomAttribute <ForeignKeyAttribute>();
            var            stringLength = i.GetCustomAttribute <StringLengthAttribute>();
            var            require      = i.GetCustomAttribute <RequiredAttribute>();
            PropsAttribute attr;

            if (attribute == null)
            {
                attr = new PropsAttribute(i.Name);
            }
            else
            {
                attr = attribute;
            }

            if (foregnKey != null)
            {
                attr.ForeignTable = attr.ForeignTable ?? foregnKey.Name;
            }
            if (stringLength != null)
            {
                attr.MaxLength = stringLength.MaximumLength;
                attr.MinLength = stringLength.MinimumLength;
            }
            if (require != null)
            {
                attr.Required = true;
            }
            return(attr);
        }