diff --git a/src/AspNetCore.Identity.Mongo/Mongo/ObjectIdConverter.cs b/src/AspNetCore.Identity.Mongo/Mongo/ObjectIdConverter.cs index edf8c8d..dd05105 100644 --- a/src/AspNetCore.Identity.Mongo/Mongo/ObjectIdConverter.cs +++ b/src/AspNetCore.Identity.Mongo/Mongo/ObjectIdConverter.cs @@ -16,7 +16,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT return base.CanConvertFrom(context, sourceType); } - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { diff --git a/src/AspNetCore.Identity.Mongo/Stores/RoleStore.cs b/src/AspNetCore.Identity.Mongo/Stores/RoleStore.cs index 9a4560c..f8a7e4c 100644 --- a/src/AspNetCore.Identity.Mongo/Stores/RoleStore.cs +++ b/src/AspNetCore.Identity.Mongo/Stores/RoleStore.cs @@ -2,7 +2,6 @@ using AspNetCore.Identity.Mongo.Mongo; using Microsoft.AspNetCore.Identity; using MongoDB.Driver; -using MongoDB.Driver.Linq; using System; using System.Collections.Generic; using System.ComponentModel; @@ -16,7 +15,8 @@ namespace AspNetCore.Identity.Mongo.Stores; /// /// Creates a new instance of a persistence store for roles. /// -/// The type of the class representing a role +/// The type of the class representing a role. +/// The type of the primary key for a user/role. public class RoleStore : IRoleClaimStore, IQueryableRoleStore @@ -183,14 +183,14 @@ public Task GetNormalizedRoleNameAsync(TRole role, CancellationToken can /// The normalized name to set /// The used to propagate notifications that the operation should be canceled. /// The that represents the asynchronous operation. - public Task SetNormalizedRoleNameAsync(TRole role, string normalizedRoleName, CancellationToken cancellationToken = default) + public Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) throw new ArgumentNullException(nameof(role)); - role.NormalizedName = normalizedRoleName; + role.NormalizedName = normalizedName; return Task.CompletedTask; } @@ -201,12 +201,12 @@ public Task SetNormalizedRoleNameAsync(TRole role, string normalizedRoleName, Ca /// The role ID to look for. /// The used to propagate notifications that the operation should be canceled. /// A that result of the look up. - public Task FindByIdAsync(string roleId, CancellationToken cancellationToken = default) + public Task FindByIdAsync(string id, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); - return _collection.FirstOrDefaultAsync(x => x.Id.Equals(ConvertIdFromString(roleId)), cancellationToken: cancellationToken); + return _collection.FirstOrDefaultAsync(x => x.Id.Equals(ConvertIdFromString(id)), cancellationToken: cancellationToken); } /// @@ -309,7 +309,7 @@ public virtual TKey ConvertIdFromString(string id) { if (id == null) { - return default(TKey); + return default; } return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id); } @@ -321,7 +321,7 @@ public virtual TKey ConvertIdFromString(string id) /// An representation of the provided . public virtual string ConvertIdToString(TKey id) { - if (object.Equals(id, default(TKey))) + if (Equals(id, default(TKey))) { return null; } diff --git a/src/AspNetCore.Identity.Mongo/Stores/UserStore.cs b/src/AspNetCore.Identity.Mongo/Stores/UserStore.cs index 173601e..7fb7b7e 100644 --- a/src/AspNetCore.Identity.Mongo/Stores/UserStore.cs +++ b/src/AspNetCore.Identity.Mongo/Stores/UserStore.cs @@ -2,12 +2,10 @@ using AspNetCore.Identity.Mongo.Mongo; using Microsoft.AspNetCore.Identity; using MongoDB.Driver; -using MongoDB.Driver.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; -using System.Linq.Expressions; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; @@ -430,7 +428,7 @@ public async Task> GetClaimsAsync(TUser user, CancellationToken can if (user == null) throw new ArgumentNullException(nameof(user)); var dbUser = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true); - return dbUser?.Claims?.Select(x => new Claim(x.ClaimType, x.ClaimValue))?.ToList() ?? new List(); + return dbUser?.Claims?.Select(x => new Claim(x.ClaimType, x.ClaimValue)).ToList() ?? new List(); } /// @@ -615,7 +613,7 @@ public Task GetAccessFailedCountAsync(TUser user, CancellationToken cancell } /// - /// Retrieves a flag indicating whether user lockout can enabled for the specified user. + /// Retrieves a flag indicating whether user lockout is enabled for the specified user. /// /// The user whose ability to be locked out should be returned. /// The used to propagate notifications that the operation should be canceled. @@ -633,7 +631,7 @@ public Task GetLockoutEnabledAsync(TUser user, CancellationToken cancellat } /// - /// Records that a failed access has occurred, incrementing the failed access count. + /// Records that failed access has occurred, incrementing the failed access count. /// /// The user whose cancellation count should be incremented. /// The used to propagate notifications that the operation should be canceled. @@ -806,7 +804,7 @@ public async Task> GetLoginsAsync(TUser user, CancellationT var dbUser = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true); - return dbUser?.Logins?.Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey, x.ProviderDisplayName))?.ToList() ?? new List(); + return dbUser?.Logins?.Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey, x.ProviderDisplayName)).ToList() ?? new List(); } /// @@ -946,7 +944,7 @@ public async Task AddToRoleAsync(TUser user, string normalizedRoleName, Cancella ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException("Value cannot be null or empty.", nameof(normalizedRoleName)); + if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException(nameof(normalizedRoleName)); var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken); if (roleEntity == null) @@ -970,7 +968,7 @@ public async Task RemoveFromRoleAsync(TUser user, string normalizedRoleName, Can ThrowIfDisposed(); if (user == null) throw new ArgumentNullException(nameof(user)); - if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException("Value cannot be null or empty.", nameof(normalizedRoleName)); + if (string.IsNullOrWhiteSpace(normalizedRoleName)) throw new ArgumentNullException(nameof(normalizedRoleName)); var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken); if (roleEntity != null) @@ -1119,7 +1117,7 @@ public async Task RedeemCodeAsync(TUser user, string code, CancellationTok if (user == null) throw new ArgumentNullException(nameof(user)); if (code == null) throw new ArgumentNullException(nameof(code)); - var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? ""; + var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? string.Empty; var splitCodes = mergedCodes.Split(';'); if (splitCodes.Contains(code)) { @@ -1137,7 +1135,7 @@ public async Task RedeemCodeAsync(TUser user, string code, CancellationTok /// /// The user who owns the recovery code. /// The used to propagate notifications that the operation should be canceled. - /// The number of valid recovery codes for the user.. + /// The number of valid recovery codes for the user. public async Task CountCodesAsync(TUser user, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); @@ -1145,7 +1143,7 @@ public async Task CountCodesAsync(TUser user, CancellationToken cancellatio if (user == null) throw new ArgumentNullException(nameof(user)); - var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? ""; + var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken) ?? string.Empty; if (mergedCodes.Length > 0) { return mergedCodes.Split(';').Length; @@ -1221,8 +1219,9 @@ public virtual TKey ConvertIdFromString(string id) { if (id == null) { - return default(TKey); + return default; } + return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id); } @@ -1233,10 +1232,11 @@ public virtual TKey ConvertIdFromString(string id) /// An representation of the provided . public virtual string ConvertIdToString(TKey id) { - if (object.Equals(id, default(TKey))) + if (Equals(id, default(TKey))) { return null; } + return id.ToString(); } @@ -1258,24 +1258,6 @@ private async Task> FindTokenAsync(TUser user, string return dbUser?.Tokens?.FirstOrDefault(x => x.LoginProvider == loginProvider && x.Name == name); } - private async Task UpdateAsync(TUser user, Expression> expression, TFieldValue value, CancellationToken cancellationToken = default) - { - if (user == null) throw new ArgumentNullException(nameof(user)); - - var updateDefinition = Builders.Update.Set(expression, value); - - await _userCollection.UpdateOneAsync(x => x.Id.Equals(user.Id), updateDefinition, cancellationToken: cancellationToken).ConfigureAwait(false); - } - - private async Task AddAsync(TUser user, Expression>> expression, TFieldValue value, CancellationToken cancellationToken = default) - { - if (user == null) throw new ArgumentNullException(nameof(user)); - - var addDefinition = Builders.Update.AddToSet(expression, value); - - await _userCollection.UpdateOneAsync(x => x.Id.Equals(user.Id), addDefinition, cancellationToken: cancellationToken).ConfigureAwait(false); - } - private Task ByIdAsync(TKey id, CancellationToken cancellationToken = default) { return _userCollection.FirstOrDefaultAsync(x => x.Id.Equals(id), cancellationToken);