public DecryptMiddleware(RequestDelegate next, IConfiguration configuration) { this.next = next; using TextReader reader = new StringReader(configuration.GetPivateKey()); PemObject pemObject = new PemReader(reader).ReadPemObject(); AsymmetricKeyParameter pkey = PrivateKeyFactory.CreateKey(pemObject.Content); this.pkcs1Encoding = new Pkcs1Encoding(new RsaEngine()); pkcs1Encoding.Init(false, pkey); var assembly = Assembly.GetExecutingAssembly(); Type[] types = assembly.GetTypes(); Type[] apicontrollers = types.Where(type => type.GetCustomAttribute <ApiControllerAttribute>() != null).ToArray(); foreach (Type ctrl in apicontrollers) { MethodInfo[] controllerMethods = ctrl.GetMethods(); RouteAttribute apiRoute = ctrl.GetCustomAttribute <RouteAttribute>(); if (apiRoute == null) { throw new Exception($"{ctrl.Name} doesn't have a \"Route\" Attribute"); } string ctrRoute = apiRoute.Template; foreach (MethodInfo meth in controllerMethods) { EncryptAttribute encryptAttribute = meth.GetCustomAttribute <EncryptAttribute>(); if (encryptAttribute != null) { string route; HttpPostAttribute post = meth.GetCustomAttribute <HttpPostAttribute>(); if (post != null) { route = post.Template; } else { throw new Exception("Api Mehtod Be Encrypted Http Method Must Be POST "); } string key = "/" + ctrRoute + "/" + route; if (this.ApiMap.ContainsKey(key)) { throw new Exception("Api Method Route Repeat Exception!"); } this.ApiMap.Add(key, encryptAttribute); } } } }
static string WriteProperty(EncryptAttribute encryptAttrib, object value) { var builder = new StringBuilder(); if (value.ToString() != value.GetType().ToString()) { builder.AppendQuoted(value.ToString()); } WriteProperties(value, (key, val) => builder.Append(builder.Length > 0 ? "; " : String.Empty) .AppendQuoted(key) .Append("=") .Append(val)); return(encryptAttrib != null ? new StringBuilder().AppendQuoted( Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(builder.ToString()), null, DataProtectionScope.CurrentUser))) .ToString() : builder.ToString()); }
private static void AddAttributes(ResultBox box, ValueHandler valueHandler, OptionAttribute optionAttribute, EncryptAttribute encryptAttribute) { object defaultValue = null; if (optionAttribute != null) { if (optionAttribute.Alias != null) { box.StoreByName = optionAttribute.Alias; } //validate that types for default value match Type dvt = optionAttribute.DefaultValue?.GetType(); if (optionAttribute.DefaultValue != null) { if (dvt != box.ResultType && dvt != typeof(string)) { throw new InvalidCastException($"Default value for option {box.Name} is of type {dvt.FullName} whereas the property has type {box.Name}. To fix this, either set default value to type {box.ResultType.FullName} or a string parseable to the target type."); } if (box.ResultType != typeof(string) && dvt == typeof(string)) { valueHandler.TryParse(box.ResultType, (string)optionAttribute.DefaultValue, out defaultValue); } } if (defaultValue == null) { defaultValue = optionAttribute.DefaultValue; } } if (defaultValue == null) { defaultValue = GetDefaultValue(box.ResultType); } box.DefaultResult = defaultValue; if (encryptAttribute != null) { box.ShouldEncrypt = true; } }