adding more comments and deleting unused methods.
This commit is contained in:
+145
-160
@@ -69,51 +69,53 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A document representing an <see cref="IdentityUser{TKey}"/> document.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||||
public class MongoIdentityUser<TKey> : IdentityUser<TKey>, IDocument<TKey>, IClaimHolder
|
public class MongoIdentityUser<TKey> : IdentityUser<TKey>, IDocument<TKey>, IClaimHolder
|
||||||
where TKey : IEquatable<TKey>
|
where TKey : IEquatable<TKey>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The version of the schema do the <see cref="MongoIdentityUser{TKey}"/> document.
|
||||||
|
/// </summary>
|
||||||
public int Version { get; set; }
|
public int Version { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The date and time at which this user was created, in UTC.
|
||||||
|
/// </summary>
|
||||||
public DateTime CreatedOn { get; private set; }
|
public DateTime CreatedOn { get; private set; }
|
||||||
public DateTime? LockoutEndDate { get; private set; }
|
/// <summary>
|
||||||
public DateTime? DeletedOn { get; private set; }
|
/// The claims this user has.
|
||||||
|
/// </summary>
|
||||||
public List<MongoClaim> Claims { get; set; }
|
public List<MongoClaim> Claims { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The role Ids of the roles that this user has.
|
||||||
|
/// </summary>
|
||||||
public List<TKey> Roles { get; set; }
|
public List<TKey> Roles { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The list of <see cref="UserLoginInfo"/>s that this user has.
|
||||||
|
/// </summary>
|
||||||
public List<UserLoginInfo> Logins { get; set; }
|
public List<UserLoginInfo> Logins { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// The list of <see cref="Token"/>s that this user has.
|
||||||
|
/// </summary>
|
||||||
public List<Token> Tokens { get; set; }
|
public List<Token> Tokens { get; set; }
|
||||||
|
|
||||||
private void InitializeFields()
|
/// <summary>
|
||||||
{
|
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username and an email address.
|
||||||
Claims = new List<MongoClaim>();
|
/// </summary>
|
||||||
Logins = new List<UserLoginInfo>();
|
|
||||||
Roles = new List<TKey>();
|
|
||||||
Tokens = new List<Token>();
|
|
||||||
Guid guidValue = Guid.NewGuid();
|
|
||||||
|
|
||||||
var idTypeName = typeof(TKey).Name;
|
|
||||||
switch (idTypeName)
|
|
||||||
{
|
|
||||||
case "Guid":
|
|
||||||
Id = (TKey)(object)guidValue;
|
|
||||||
break;
|
|
||||||
case "Int32":
|
|
||||||
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
|
||||||
break;
|
|
||||||
case "String":
|
|
||||||
Id = (TKey)(object)guidValue.ToString();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public MongoIdentityUser()
|
public MongoIdentityUser()
|
||||||
{
|
{
|
||||||
CreatedOn = DateTime.UtcNow;
|
|
||||||
SetVersion(1);
|
SetVersion(1);
|
||||||
InitializeFields();
|
InitializeFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username and an email address.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userName">The name of the user.</param>
|
||||||
|
/// <param name="email">The email address of the user.</param>
|
||||||
public MongoIdentityUser(string userName, string email) : this(userName)
|
public MongoIdentityUser(string userName, string email) : this(userName)
|
||||||
{
|
{
|
||||||
if (email != null)
|
if (email != null)
|
||||||
@@ -122,118 +124,40 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The constructor for a <see cref="MongoIdentityUser{TKey}"/>, taking a username.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userName">The name of the user.</param>
|
||||||
public MongoIdentityUser(string userName)
|
public MongoIdentityUser(string userName)
|
||||||
{
|
{
|
||||||
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
||||||
CreatedOn = DateTime.UtcNow;
|
|
||||||
|
|
||||||
SetVersion(1);
|
SetVersion(1);
|
||||||
InitializeFields();
|
InitializeFields();
|
||||||
Roles = new List<TKey>();
|
Roles = new List<TKey>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual MongoIdentityUser<TKey> SetId(TKey key)
|
/// <summary>
|
||||||
{
|
/// Sets the version of the schema for the <see cref="MongoIdentityUser{TKey}"/> document.
|
||||||
Id = key;
|
/// </summary>
|
||||||
return this;
|
/// <param name="version"></param>
|
||||||
}
|
/// <returns></returns>
|
||||||
|
|
||||||
public virtual MongoIdentityUser<TKey> SetVersion(int version)
|
public virtual MongoIdentityUser<TKey> SetVersion(int version)
|
||||||
{
|
{
|
||||||
Version = 1;
|
Version = 1;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void EnableTwoFactorAuthentication()
|
|
||||||
{
|
|
||||||
TwoFactorEnabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void DisableTwoFactorAuthentication()
|
|
||||||
{
|
|
||||||
TwoFactorEnabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void EnableLockout()
|
|
||||||
{
|
|
||||||
LockoutEnabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void DisableLockout()
|
|
||||||
{
|
|
||||||
LockoutEnabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetEmail(string email)
|
|
||||||
{
|
|
||||||
Email = email ?? throw new ArgumentNullException(nameof(email));
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetNormalizedUserName(string normalizedUserName)
|
|
||||||
{
|
|
||||||
NormalizedUserName = normalizedUserName ?? throw new ArgumentNullException(nameof(normalizedUserName));
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetPhoneNumber(string phoneNumber)
|
|
||||||
{
|
|
||||||
PhoneNumber = phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetPasswordHash(string passwordHash)
|
|
||||||
{
|
|
||||||
PasswordHash = passwordHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetSecurityStamp(string securityStamp)
|
|
||||||
{
|
|
||||||
SecurityStamp = securityStamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void SetAccessFailedCount(int accessFailedCount)
|
|
||||||
{
|
|
||||||
AccessFailedCount = accessFailedCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void ResetAccessFailedCount()
|
|
||||||
{
|
|
||||||
AccessFailedCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void LockUntil(DateTime lockoutEndDate)
|
|
||||||
{
|
|
||||||
LockoutEndDate = lockoutEndDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Delete()
|
|
||||||
{
|
|
||||||
if (DeletedOn != null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"User '{Id}' has already been deleted.");
|
|
||||||
}
|
|
||||||
|
|
||||||
DeletedOn = DateTime.UtcNow;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Role Management
|
#region Role Management
|
||||||
|
|
||||||
public virtual IdentityUserRole<TKey> GetUserRole(TKey roleId)
|
/// <summary>
|
||||||
{
|
/// Removes a role.
|
||||||
var foundRoleId = Roles.FirstOrDefault(e => e.Equals(roleId));
|
/// </summary>
|
||||||
if (!foundRoleId.Equals(default(TKey)))
|
/// <param name="roleId">The Id of the role you want to remove.</param>
|
||||||
{
|
/// <returns>True if the removal was successful.</returns>
|
||||||
return new IdentityUserRole<TKey>
|
|
||||||
{
|
|
||||||
UserId = Id,
|
|
||||||
RoleId = foundRoleId
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return default(IdentityUserRole<TKey>);
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual bool RemoveRole(TKey roleId)
|
public virtual bool RemoveRole(TKey roleId)
|
||||||
{
|
{
|
||||||
var roleClaim = Roles.FirstOrDefault(e => e.Equals(roleId));
|
var roleClaim = Roles.FirstOrDefault(e => e.Equals(roleId));
|
||||||
if (!roleClaim.Equals(default(TKey)))
|
if (roleClaim != null && !roleClaim.Equals(default(TKey)))
|
||||||
{
|
{
|
||||||
Roles.Remove(roleId);
|
Roles.Remove(roleId);
|
||||||
return true;
|
return true;
|
||||||
@@ -241,8 +165,18 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a role to the user.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roleId">The Id of the role you want to add.</param>
|
||||||
|
/// <returns>True if the addition was successful.</returns>
|
||||||
public virtual bool AddRole(TKey roleId)
|
public virtual bool AddRole(TKey roleId)
|
||||||
{
|
{
|
||||||
|
if(roleId == null || roleId.Equals(default(TKey)))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(roleId));
|
||||||
|
}
|
||||||
if (!Roles.Contains(roleId))
|
if (!Roles.Contains(roleId))
|
||||||
{
|
{
|
||||||
Roles.Add(roleId);
|
Roles.Add(roleId);
|
||||||
@@ -255,35 +189,60 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
|
|
||||||
#region Login Management
|
#region Login Management
|
||||||
|
|
||||||
public virtual bool AddLogin(UserLoginInfo mongoUserLogin)
|
/// <summary>
|
||||||
|
/// Adds a user login to the user.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userLoginInfo">The <see cref="UserLoginInfo"/> you want to add.</param>
|
||||||
|
/// <returns>True if the addition was successful.</returns>
|
||||||
|
public virtual bool AddLogin(UserLoginInfo userLoginInfo)
|
||||||
{
|
{
|
||||||
if (mongoUserLogin == null)
|
if (userLoginInfo == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(mongoUserLogin));
|
throw new ArgumentNullException(nameof(userLoginInfo));
|
||||||
}
|
}
|
||||||
if (HasLogin(mongoUserLogin))
|
if (HasLogin(userLoginInfo))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Logins.Add(mongoUserLogin);
|
Logins.Add(userLoginInfo);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool HasLogin(UserLoginInfo login)
|
/// <summary>
|
||||||
|
/// Checks if the user has the given <see cref="UserLoginInfo"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userLoginInfo">The <see cref="UserLoginInfo"/> we are looking for.</param>
|
||||||
|
/// <returns>True if the user has the given <see cref="UserLoginInfo"/>.</returns>
|
||||||
|
public virtual bool HasLogin(UserLoginInfo userLoginInfo)
|
||||||
{
|
{
|
||||||
return Logins.Any(e => e.LoginProvider == login.LoginProvider && e.ProviderKey == e.ProviderKey);
|
return Logins.Any(e => e.LoginProvider == userLoginInfo.LoginProvider && e.ProviderKey == e.ProviderKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void RemoveLogin(UserLoginInfo mongoUserLogin)
|
/// <summary>
|
||||||
|
/// Removes a <see cref="UserLoginInfo"/> from the user.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userLoginInfo"></param>
|
||||||
|
public virtual bool RemoveLogin(UserLoginInfo userLoginInfo)
|
||||||
{
|
{
|
||||||
if (mongoUserLogin == null)
|
if (userLoginInfo == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(mongoUserLogin));
|
throw new ArgumentNullException(nameof(userLoginInfo));
|
||||||
}
|
}
|
||||||
|
var loginToremove = Logins.FirstOrDefault(e => e.LoginProvider == userLoginInfo.LoginProvider && e.ProviderKey == e.ProviderKey);
|
||||||
Logins.Remove(mongoUserLogin);
|
if (loginToremove != null)
|
||||||
|
{
|
||||||
|
Logins.Remove(loginToremove);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="loginProvider"></param>
|
||||||
|
/// <param name="providerKey"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public virtual IdentityUserLogin<TKey> GetUserLogin(string loginProvider, string providerKey)
|
public virtual IdentityUserLogin<TKey> GetUserLogin(string loginProvider, string providerKey)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -307,12 +266,11 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replaces a claim on a claim holder, implementing <see cref="IClaimHolder"/>.
|
/// Sets the token to a new value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="claimHolder">The object holding claims.</param>
|
/// <param name="tokenToset">The token you want to set you want to set.</param>
|
||||||
/// <param name="claim">The claim you want to replace.</param>
|
/// <param name="value">The value you want to set the token to.</param>
|
||||||
/// <param name="newClaim">The new claim you want to set.</param>
|
/// <returns>Returns true if the token was successfully set.</returns>
|
||||||
/// <returns>Returns true if the claim was replaced.</returns>
|
|
||||||
public bool SetToken(IdentityUserToken<TKey> tokenToset, string value)
|
public bool SetToken(IdentityUserToken<TKey> tokenToset, string value)
|
||||||
{
|
{
|
||||||
var token = Tokens.FirstOrDefault(e => e.LoginProvider == tokenToset.LoginProvider && e.Name == tokenToset.Name);
|
var token = Tokens.FirstOrDefault(e => e.LoginProvider == tokenToset.LoginProvider && e.Name == tokenToset.Name);
|
||||||
@@ -324,6 +282,12 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a token given the login provider and the name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="loginProvider">The value for the login provider.</param>
|
||||||
|
/// <param name="name">The name of the token.</param>
|
||||||
|
/// <returns>An <see cref="IdentityUser{TKey}"/> if found, or null.</returns>
|
||||||
public IdentityUserToken<TKey> GetToken(string loginProvider, string name)
|
public IdentityUserToken<TKey> GetToken(string loginProvider, string name)
|
||||||
{
|
{
|
||||||
var token = Tokens.FirstOrDefault(e => e.LoginProvider == loginProvider && e.Name == name);
|
var token = Tokens.FirstOrDefault(e => e.LoginProvider == loginProvider && e.Name == name);
|
||||||
@@ -340,6 +304,11 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
return default(IdentityUserToken<TKey>);
|
return default(IdentityUserToken<TKey>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if a user has the given token.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token">The token you are looking for.</param>
|
||||||
|
/// <returns>True if the user has the given token</returns>
|
||||||
public bool HasToken(IdentityUserToken<TKey> token)
|
public bool HasToken(IdentityUserToken<TKey> token)
|
||||||
{
|
{
|
||||||
return Tokens.Any(e => e.LoginProvider == token.LoginProvider
|
return Tokens.Any(e => e.LoginProvider == token.LoginProvider
|
||||||
@@ -347,26 +316,12 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
&& e.Value == token.Value);
|
&& e.Value == token.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool AddOrSet(IdentityUserToken<TKey> token)
|
/// <summary>
|
||||||
{
|
/// Adds a token to the user.
|
||||||
var exists = GetToken(token.LoginProvider, token.Name);
|
/// </summary>
|
||||||
if (exists != null && exists.Value != token.Value)
|
/// <typeparam name="TUserToken">The type of the token.</typeparam>
|
||||||
{
|
/// <param name="token">The token you want to add.</param>
|
||||||
return SetToken(exists, token.Value);
|
/// <returns>True if the addition was successful.</returns>
|
||||||
}
|
|
||||||
if (exists == null)
|
|
||||||
{
|
|
||||||
Tokens.Add(new Token
|
|
||||||
{
|
|
||||||
LoginProvider = token.LoginProvider,
|
|
||||||
Name = token.Name,
|
|
||||||
Value = token.Value
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool AddUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
|
public bool AddUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
|
||||||
{
|
{
|
||||||
if (HasToken(token))
|
if (HasToken(token))
|
||||||
@@ -383,9 +338,16 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes a token from the user.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TUserToken">The type of the token.</typeparam>
|
||||||
|
/// <param name="token">The token you want to remove.</param>
|
||||||
|
/// <returns>True if the removal was successful.</returns>
|
||||||
public bool RemoveUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
|
public bool RemoveUserToken<TUserToken>(TUserToken token) where TUserToken : IdentityUserToken<TKey>
|
||||||
{
|
{
|
||||||
var exists = Tokens.FirstOrDefault(e => e.LoginProvider == token.LoginProvider && e.Name == token.Name);
|
var exists = Tokens.FirstOrDefault(e => e.LoginProvider == token.LoginProvider
|
||||||
|
&& e.Name == token.Name);
|
||||||
if (exists == null)
|
if (exists == null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -396,7 +358,30 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
|||||||
|
|
||||||
#endregion Token Management
|
#endregion Token Management
|
||||||
|
|
||||||
|
private void InitializeFields()
|
||||||
|
{
|
||||||
|
CreatedOn = DateTime.UtcNow;
|
||||||
|
Claims = new List<MongoClaim>();
|
||||||
|
Logins = new List<UserLoginInfo>();
|
||||||
|
Roles = new List<TKey>();
|
||||||
|
Tokens = new List<Token>();
|
||||||
|
Guid guidValue = Guid.NewGuid();
|
||||||
|
|
||||||
|
var idTypeName = typeof(TKey).Name;
|
||||||
|
switch (idTypeName)
|
||||||
|
{
|
||||||
|
case "Guid":
|
||||||
|
Id = (TKey)(object)guidValue;
|
||||||
|
break;
|
||||||
|
case "Int32":
|
||||||
|
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
||||||
|
break;
|
||||||
|
case "String":
|
||||||
|
Id = (TKey)(object)guidValue.ToString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
public string LoginProvider { get; set; }
|
public string LoginProvider { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the name of the token.
|
/// Gets or sets the name of the token.
|
||||||
|
/// </summary>
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the token value.
|
/// Gets or sets the token value.
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
using AspNetCore.Identity.MongoDbCore.Infrastructure;
|
|
||||||
using AspNetCore.Identity.MongoDbCore.Models;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
using MongoDbGenericRepository;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace AspNetCore.Identity.MongoDbCore
|
|
||||||
{
|
|
||||||
|
|
||||||
public class MongoIdentityDbContext : MongoDbContext
|
|
||||||
{
|
|
||||||
|
|
||||||
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoRepository MongoRepository { get; }
|
|
||||||
|
|
||||||
public IMongoCollection<MongoIdentityUser> Users
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<MongoIdentityUser>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoCollection<MongoIdentityRole> Roles
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<MongoIdentityRole>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MongoIdentityDbContext<TUser> : MongoDbContext
|
|
||||||
where TUser : MongoIdentityUser
|
|
||||||
{
|
|
||||||
|
|
||||||
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoRepository MongoRepository { get; }
|
|
||||||
|
|
||||||
public IMongoCollection<TUser> Users
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<TUser>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoCollection<MongoIdentityRole> Roles
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<MongoIdentityRole>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MongoIdentityDbContext<TUser, TRole, TKey> : MongoDbContext
|
|
||||||
where TUser : MongoIdentityUser<TKey>
|
|
||||||
where TRole : MongoIdentityRole<TKey>
|
|
||||||
where TKey : IEquatable<TKey>
|
|
||||||
{
|
|
||||||
|
|
||||||
public MongoIdentityDbContext(string connectionString, string databaseName) : base(connectionString, databaseName)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoCollection<TUser> Users
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<TUser>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMongoCollection<TRole> Roles
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return GetCollection<TRole>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -381,6 +381,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose() => _disposed = true;
|
public void Dispose() => _disposed = true;
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the claims associated with the specified <paramref name="role"/> as an asynchronous operation.
|
/// Get the claims associated with the specified <paramref name="role"/> as an asynchronous operation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -388,6 +389,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
||||||
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a role.</returns>
|
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a role.</returns>
|
||||||
public async virtual Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
public async virtual Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
if (role == null)
|
if (role == null)
|
||||||
|
|||||||
@@ -297,6 +297,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
return Task.FromResult(default(TUserLogin));
|
return Task.FromResult(default(TUserLogin));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
|
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -304,6 +305,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
||||||
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
|
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
|
||||||
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
if (user == null)
|
if (user == null)
|
||||||
@@ -435,6 +437,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
return Task.FromResult(false);
|
return Task.FromResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
|
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -444,6 +447,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
||||||
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
|
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
|
||||||
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
|
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
CancellationToken cancellationToken = default(CancellationToken))
|
CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
@@ -459,6 +463,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves the associated logins for the specified <param ref="user"/>.
|
/// Retrieves the associated logins for the specified <param ref="user"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -468,6 +473,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
|
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
|
|||||||
@@ -475,6 +475,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
|
/// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -482,6 +483,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
||||||
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
|
/// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns>
|
||||||
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
public async override Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
if (user == null)
|
if (user == null)
|
||||||
@@ -613,6 +615,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
return Task.FromResult(false);
|
return Task.FromResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
|
/// Removes the <paramref name="loginProvider"/> given from the specified <paramref name="user"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -622,6 +625,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
|
||||||
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
|
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
|
||||||
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
|
public override async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
CancellationToken cancellationToken = default(CancellationToken))
|
CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
@@ -637,6 +641,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves the associated logins for the specified <param ref="user"/>.
|
/// Retrieves the associated logins for the specified <param ref="user"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -646,6 +651,7 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
|
/// The <see cref="Task"/> for the asynchronous operation, containing a list of <see cref="UserLoginInfo"/> for the specified <paramref name="user"/>, if any.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
public async override Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
|
#pragma warning restore CS1998 // Cette méthode async n'a pas d'opérateur 'await' et elle s'exécutera de façon synchrone
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
@@ -745,10 +751,6 @@ namespace AspNetCore.Identity.MongoDbCore
|
|||||||
|
|
||||||
#region Token Management
|
#region Token Management
|
||||||
|
|
||||||
public class TokenProjection
|
|
||||||
{
|
|
||||||
public List<Token> Tokens { get; set; }
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find a user token if it exists.
|
/// Find a user token if it exists.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user