PR recommendations: added null checks where FindByX was added, using the same exception as ASP.NET Identity 2

This commit is contained in:
Scott Brady
2020-04-30 15:48:32 +01:00
parent 643c249386
commit 077cf57abc
5 changed files with 10 additions and 32 deletions

View File

@@ -131,6 +131,7 @@ namespace Umbraco.Web.Editors
public async Task<HttpResponseMessage> PostUnLinkLogin(UnLinkLoginModel unlinkLoginModel)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user == null) throw new InvalidOperationException("Could not find user");
var result = await UserManager.RemoveLoginAsync(
user,

View File

@@ -329,6 +329,7 @@ namespace Umbraco.Web.Editors
}
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user == null) throw new InvalidOperationException("Could not find user");
var result = await UserManager.AddLoginAsync(user,
new UserLoginInfo(loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey, loginInfo.Login.LoginProvider));

View File

@@ -159,6 +159,8 @@ namespace Umbraco.Web.Editors
public async Task<UserDetail> PostSetInvitedUserPassword([FromBody]string newPassword)
{
var user = await UserManager.FindByIdAsync(Security.GetUserId().ResultOr(0).ToString());
if (user == null) throw new InvalidOperationException("Could not find user");
var result = await UserManager.AddPasswordAsync(user, newPassword);
if (result.Succeeded == false)

View File

@@ -306,6 +306,8 @@ namespace Umbraco.Web.Security
public async Task<IdentityResult> ChangePasswordWithResetAsync(int userId, string token, string newPassword)
{
var user = await base.FindByIdAsync(userId.ToString());
if (user == null) throw new InvalidOperationException("Could not find user");
var result = await base.ResetPasswordAsync(user, token, newPassword);
if (result.Succeeded) RaisePasswordChangedEvent(userId);
return result;
@@ -317,24 +319,7 @@ namespace Umbraco.Web.Security
if (result.Succeeded) RaisePasswordChangedEvent(user.Id);
return result;
}
/// <summary>
/// Override to determine how to hash the password
/// </summary>
/// <param name="store"></param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <returns></returns>
protected override async Task<PasswordVerificationResult> VerifyPasswordAsync(IUserPasswordStore<T> store, T user, string password)
{
var userAwarePasswordHasher = PasswordHasher;
if (userAwarePasswordHasher == null)
return await base.VerifyPasswordAsync(store, user, password);
var hash = await store.GetPasswordHashAsync(user, CancellationToken.None);
return userAwarePasswordHasher.VerifyHashedPassword(user, hash, password);
}
/// <summary>
/// Override to determine how to hash the password
/// </summary>

View File

@@ -12,22 +12,14 @@ namespace Umbraco.Web.Security
where TResult : class, IDisposable
where TOptions : IdentityFactoryOptions<TResult>
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="next">The next middleware in the OWIN pipeline to invoke</param>
/// <param name="options">Configuration options for the middleware</param>
public IdentityFactoryMiddleware(OwinMiddleware next, TOptions options)
: base(next)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (options.Provider == null)
{
throw new ArgumentNullException("options.Provider");
}
if (options == null) throw new ArgumentNullException(nameof(options));
if (options.Provider == null) throw new ArgumentException("options.Provider");
Options = options;
}
@@ -74,9 +66,6 @@ namespace Umbraco.Web.Security
public class IdentityFactoryProvider<T> where T : class, IDisposable
{
/// <summary>
/// Constructor
/// </summary>
public IdentityFactoryProvider()
{
OnDispose = (options, instance) => { };