forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing FormatException when the login screen is posted with values ot…
…her than true/false for RememberMe (Lombiq Technologies: OCORE-132) (OrchardCMS#14845) * Fixing FormatException when the login screen is posted with values other than true/false for RememberMe * Changing anti-cracking attempt model binding to use a custom model binder instead * Moving SafeBoolModelBinder to OrchardCore.Mvc.Core * Sealing internal classes * Formatting Co-authored-by: Hisham Bin Ateya <[email protected]> --------- Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
Showing
5 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/OrchardCore/OrchardCore.Mvc.Core/ModelBinding/SafeBoolModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Localization; | ||
|
||
namespace OrchardCore.Mvc.ModelBinding; | ||
|
||
/// <summary> | ||
/// Model binder to produce a validation error when a Boolean field contains a value that's not a valid bool. The | ||
/// default model binder would throw a <see cref="FormatException"/>. That's an issue for e.g. the Users module, see | ||
/// <see href="https://github.com/OrchardCMS/OrchardCore/issues/14792"/>. | ||
/// </summary> | ||
internal sealed class SafeBoolModelBinder : IModelBinder | ||
{ | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
ArgumentNullException.ThrowIfNull(nameof(bindingContext)); | ||
|
||
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); | ||
|
||
if (valueProviderResult == ValueProviderResult.None) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (bool.TryParse(valueProviderResult.FirstValue, out bool result)) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Success(result); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
var localizer = bindingContext.HttpContext.RequestServices.GetService<IStringLocalizer<SafeBoolModelBinder>>(); | ||
|
||
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, localizer["Invalid Boolean value."]); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/OrchardCore/OrchardCore.Mvc.Core/ModelBinding/SafeBoolModelBinderProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace OrchardCore.Mvc.ModelBinding; | ||
|
||
internal sealed class SafeBoolModelBinderProvider : IModelBinderProvider | ||
{ | ||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context, nameof(context)); | ||
|
||
if (context.Metadata.ModelType == typeof(bool)) | ||
{ | ||
return new SafeBoolModelBinder(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters