コード例 #1
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var logger         = bindingContext.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>();
            var fallbackBinder = new SimpleTypeModelBinder(bindingContext.ModelType, logger);

            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                return(fallbackBinder.BindModelAsync(bindingContext));
            }

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

            var stringValue = valueProviderResult.FirstValue;

            if (string.IsNullOrWhiteSpace(stringValue))
            {
                return(fallbackBinder.BindModelAsync(bindingContext));
            }

            var decryptedResult = _protection.Decrypt(stringValue);

            bindingContext.Result = ModelBindingResult.Success(decryptedResult);
            return(Task.CompletedTask);
        }