Useful .NET Encryption Utils from: https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider(v=vs.110).aspx
Пример #1
0
        public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            var metadata = new MetadataTypes {
                Config = Config,
            };
            var existingTypes = new HashSet <Type> {
                typeof(ResponseStatus),
                typeof(ErrorResponse),
            };

            foreach (var entry in EndpointHost.ServiceOperations.OperationResponseTypesMap)
            {
                metadata.Operations.Add(new MetadataOperationType {
                    Request  = entry.Key.ToType(),
                    Response = entry.Value.ToType(),
                });
                existingTypes.Add(entry.Key);
                existingTypes.Add(entry.Value);
            }

            foreach (var type in EndpointHost.ServiceOperations.AllOperations.Types)
            {
                if (existingTypes.Contains(type))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType {
                    Request = type.ToType(),
                });

                existingTypes.Add(type);
            }

            var considered = new HashSet <Type>(existingTypes);
            var queue      = new Queue <Type>(existingTypes);

            while (queue.Count > 0)
            {
                var type = queue.Dequeue();
                foreach (var pi in type.GetSerializableProperties())
                {
                    if (pi.PropertyType.IsUserType())
                    {
                        if (considered.Contains(pi.PropertyType))
                        {
                            continue;
                        }

                        considered.Add(pi.PropertyType);
                        queue.Enqueue(pi.PropertyType);
                        metadata.Types.Add(pi.PropertyType.ToType());
                    }
                }

                if (type.BaseType != null &&
                    type.BaseType.IsUserType() &&
                    !considered.Contains(type.BaseType))
                {
                    considered.Add(type.BaseType);
                    queue.Enqueue(type.BaseType);
                    metadata.Types.Add(type.BaseType.ToType());
                }
            }

            httpRes.ContentType = "application/x-ssz-metatypes";
            var json    = metadata.ToJson();
            var encJson = CryptUtils.Encrypt(EndpointHostConfig.PublicKey, json, RsaKeyLengths.Bit2048);

            httpRes.Write(encJson);
        }
        public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            var metadata = new MetadataTypes
            {
                Config = Config,
            };
            var existingTypes = new HashSet <Type> {
                typeof(ResponseStatus),
                typeof(ErrorResponse),
            };

            var meta = EndpointHost.Metadata;

            foreach (var operation in meta.Operations)
            {
                if (!meta.IsVisible(httpReq, operation))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType
                {
                    Actions  = operation.Actions,
                    Request  = operation.RequestType.ToType(),
                    Response = operation.ResponseType.ToType(),
                });

                existingTypes.Add(operation.RequestType);
                if (operation.ResponseType != null)
                {
                    existingTypes.Add(operation.ResponseType);
                }
            }

            foreach (var type in meta.GetAllTypes())
            {
                if (existingTypes.Contains(type))
                {
                    continue;
                }

                metadata.Operations.Add(new MetadataOperationType
                {
                    Request = type.ToType(),
                });

                existingTypes.Add(type);
            }

            var considered = new HashSet <Type>(existingTypes);
            var queue      = new Queue <Type>(existingTypes);

            while (queue.Count > 0)
            {
                var type = queue.Dequeue();
                foreach (var pi in type.GetSerializableProperties())
                {
                    if (pi.PropertyType.IsUserType())
                    {
                        if (considered.Contains(pi.PropertyType))
                        {
                            continue;
                        }

                        considered.Add(pi.PropertyType);
                        queue.Enqueue(pi.PropertyType);
                        metadata.Types.Add(pi.PropertyType.ToType());
                    }
                }

                if (type.BaseType != null &&
                    type.BaseType.IsUserType() &&
                    !considered.Contains(type.BaseType))
                {
                    considered.Add(type.BaseType);
                    queue.Enqueue(type.BaseType);
                    metadata.Types.Add(type.BaseType.ToType());
                }
            }

            var json = metadata.ToJson();

            //httpRes.ContentType = "application/json";
            //httpRes.Write(json);
            //return;

            httpRes.ContentType = "application/x-ssz-metatypes";
            var encJson = CryptUtils.Encrypt(EndpointHostConfig.PublicKey, json, RsaKeyLengths.Bit2048);

            httpRes.Write(encJson);
        }