From 189214cae17d6255989529ff47b2d963ed4c1038 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 4 Jun 2020 00:40:19 +0800 Subject: chore(back): Fix some warnings. --- .../Models/Http/ActionContextAccessorExtensions.cs | 14 +++ Timeline/Models/Http/Timeline.cs | 15 +-- Timeline/Models/Http/UserInfo.cs | 5 +- .../Resources/Models/Http/Exception.Designer.cs | 81 +++++++++++++ Timeline/Resources/Models/Http/Exception.resx | 126 +++++++++++++++++++++ Timeline/Services/PasswordService.cs | 4 +- Timeline/Timeline.csproj | 9 ++ 7 files changed, 239 insertions(+), 15 deletions(-) create mode 100644 Timeline/Models/Http/ActionContextAccessorExtensions.cs create mode 100644 Timeline/Resources/Models/Http/Exception.Designer.cs create mode 100644 Timeline/Resources/Models/Http/Exception.resx (limited to 'Timeline') diff --git a/Timeline/Models/Http/ActionContextAccessorExtensions.cs b/Timeline/Models/Http/ActionContextAccessorExtensions.cs new file mode 100644 index 00000000..bcc55c5a --- /dev/null +++ b/Timeline/Models/Http/ActionContextAccessorExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using System; + +namespace Timeline.Models.Http +{ + public static class ActionContextAccessorExtensions + { + public static ActionContext AssertActionContextForUrlFill(this IActionContextAccessor accessor) + { + return accessor.ActionContext ?? throw new InvalidOperationException(Resources.Models.Http.Exception.ActionContextNull); + } + } +} diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs index 9e2aefd0..fb767f10 100644 --- a/Timeline/Models/Http/Timeline.cs +++ b/Timeline/Models/Http/Timeline.cs @@ -58,11 +58,8 @@ namespace Timeline.Models.Http public TimelineInfoLinks Resolve(Timeline source, TimelineInfo destination, TimelineInfoLinks destMember, ResolutionContext context) { - if (_actionContextAccessor.ActionContext == null) - throw new InvalidOperationException("No action context, can't fill urls."); - - var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); - + var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); + var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); return new TimelineInfoLinks { @@ -85,10 +82,8 @@ namespace Timeline.Models.Http public TimelinePostContentInfo Resolve(TimelinePost source, TimelinePostInfo destination, TimelinePostContentInfo destMember, ResolutionContext context) { - if (_actionContextAccessor.ActionContext == null) - throw new InvalidOperationException("No action context, can't fill urls."); - - var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); + var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); + var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); var sourceContent = source.Content; @@ -113,7 +108,7 @@ namespace Timeline.Models.Http } else { - throw new InvalidOperationException("Unknown content type."); + throw new InvalidOperationException(Resources.Models.Http.Exception.UnknownPostContentType); } } } diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs index b4bf14c1..0acb1a80 100644 --- a/Timeline/Models/Http/UserInfo.cs +++ b/Timeline/Models/Http/UserInfo.cs @@ -37,10 +37,9 @@ namespace Timeline.Models.Http public UserInfoLinks Resolve(User source, UserInfo destination, UserInfoLinks destMember, ResolutionContext context) { - if (_actionContextAccessor.ActionContext == null) - throw new InvalidOperationException("No action context, can't fill urls."); + var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); + var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); - var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); var result = new UserInfoLinks { Self = urlHelper.ActionLink(nameof(UserController.Get), nameof(UserController)[0..^nameof(Controller).Length], new { destination.Username }), diff --git a/Timeline/Resources/Models/Http/Exception.Designer.cs b/Timeline/Resources/Models/Http/Exception.Designer.cs new file mode 100644 index 00000000..19f42793 --- /dev/null +++ b/Timeline/Resources/Models/Http/Exception.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Resources.Models.Http { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Exception { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Exception() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Resources.Models.Http.Exception", typeof(Exception).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to No action context currently, can't fill urls in value resolver.. + /// + internal static string ActionContextNull { + get { + return ResourceManager.GetString("ActionContextNull", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Unknown post content type.. + /// + internal static string UnknownPostContentType { + get { + return ResourceManager.GetString("UnknownPostContentType", resourceCulture); + } + } + } +} diff --git a/Timeline/Resources/Models/Http/Exception.resx b/Timeline/Resources/Models/Http/Exception.resx new file mode 100644 index 00000000..3f7bddb6 --- /dev/null +++ b/Timeline/Resources/Models/Http/Exception.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + No action context currently, can't fill urls in value resolver. + + + Unknown post content type. + + \ No newline at end of file diff --git a/Timeline/Services/PasswordService.cs b/Timeline/Services/PasswordService.cs index e04a861b..8114a520 100644 --- a/Timeline/Services/PasswordService.cs +++ b/Timeline/Services/PasswordService.cs @@ -110,7 +110,7 @@ namespace Timeline.Services return Convert.ToBase64String(HashPasswordV3(password, _rng)); } - private byte[] HashPasswordV3(string password, RandomNumberGenerator rng) + private static byte[] HashPasswordV3(string password, RandomNumberGenerator rng) { return HashPasswordV3(password, rng, prf: KeyDerivationPrf.HMACSHA256, @@ -166,7 +166,7 @@ namespace Timeline.Services }; } - private bool VerifyHashedPasswordV3(byte[] hashedPassword, string password, string hashedPasswordString) + private static bool VerifyHashedPasswordV3(byte[] hashedPassword, string password, string hashedPasswordString) { try { diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj index f918b2d6..4f5c0f1f 100644 --- a/Timeline/Timeline.csproj +++ b/Timeline/Timeline.csproj @@ -119,6 +119,11 @@ True Common.resx + + True + True + Exception.resx + True True @@ -208,6 +213,10 @@ ResXFileCodeGenerator Common.Designer.cs + + ResXFileCodeGenerator + Exception.Designer.cs + ResXFileCodeGenerator NicknameValidator.Designer.cs -- cgit v1.2.3