Refactoring
This commit is contained in:
44
AMWD.Common.AspNetCore/Utilities/BackgroundServiceStarter.cs
Normal file
44
AMWD.Common.AspNetCore/Utilities/BackgroundServiceStarter.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Extensions.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper class to start a background service.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">The service type.</typeparam>
|
||||
public class BackgroundServiceStarter<TService> : IHostedService
|
||||
where TService : class, IHostedService
|
||||
{
|
||||
private readonly TService service;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an new instance of the <see cref="BackgroundServiceStarter{TService}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="backgroundService">The service to work in background.</param>
|
||||
public BackgroundServiceStarter(TService backgroundService)
|
||||
{
|
||||
service = backgroundService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the service.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return service.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the service.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return service.StopAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
52
AMWD.Common.AspNetCore/Utilities/PasswordHelper.cs
Normal file
52
AMWD.Common.AspNetCore/Utilities/PasswordHelper.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
namespace Microsoft.AspNetCore.Identity
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides password hashing and verification methods.
|
||||
/// </summary>
|
||||
public static class PasswordHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Hashes a password.
|
||||
/// </summary>
|
||||
/// <param name="plainPassword">The plain password.</param>
|
||||
/// <returns></returns>
|
||||
public static string HashPassword(string plainPassword)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(plainPassword))
|
||||
return plainPassword?.Trim();
|
||||
|
||||
var ph = new PasswordHasher<object>();
|
||||
return ph.HashPassword(null, plainPassword.Trim());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies a password with a hashed version.
|
||||
/// </summary>
|
||||
/// <param name="plainPassword">The plain password.</param>
|
||||
/// <param name="hashedPassword">The password hash.</param>
|
||||
/// <param name="rehashNeeded">A value indicating whether the password needs a rehash.</param>
|
||||
/// <returns></returns>
|
||||
public static bool VerifyPassword(string plainPassword, string hashedPassword, out bool rehashNeeded)
|
||||
{
|
||||
rehashNeeded = false;
|
||||
if (string.IsNullOrWhiteSpace(plainPassword) || string.IsNullOrWhiteSpace(hashedPassword))
|
||||
return false;
|
||||
|
||||
var ph = new PasswordHasher<object>();
|
||||
var result = ph.VerifyHashedPassword(null, hashedPassword, plainPassword);
|
||||
switch (result)
|
||||
{
|
||||
case PasswordVerificationResult.Success:
|
||||
return true;
|
||||
|
||||
case PasswordVerificationResult.SuccessRehashNeeded:
|
||||
rehashNeeded = true;
|
||||
return true;
|
||||
|
||||
case PasswordVerificationResult.Failed:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user