use IDocumentStore instead of IAsyncDocumentSession
This commit is contained in:
@@ -18,9 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||
Readme.md = Readme.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.RazorPages", "Samples\RazorPages\Sample.RazorPages.csproj", "{0C8F67DC-B6B3-4D8C-9E33-BBB47F858BE3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Mvc", "Samples\Mvc\Sample.Mvc.csproj", "{8AA88A38-C74A-4F0D-9853-EFA0D78F0E63}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Mvc", "Samples\Mvc\Sample.Mvc.csproj", "{8AA88A38-C74A-4F0D-9853-EFA0D78F0E63}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -32,10 +30,6 @@ Global
|
||||
{C7824FBF-9E9B-40B5-9CF5-4884F8378BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C7824FBF-9E9B-40B5-9CF5-4884F8378BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C7824FBF-9E9B-40B5-9CF5-4884F8378BA6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0C8F67DC-B6B3-4D8C-9E33-BBB47F858BE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0C8F67DC-B6B3-4D8C-9E33-BBB47F858BE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0C8F67DC-B6B3-4D8C-9E33-BBB47F858BE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0C8F67DC-B6B3-4D8C-9E33-BBB47F858BE3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8AA88A38-C74A-4F0D-9853-EFA0D78F0E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8AA88A38-C74A-4F0D-9853-EFA0D78F0E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8AA88A38-C74A-4F0D-9853-EFA0D78F0E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
|
||||
<PackageReference Include="RavenDB.Client" Version="4.2.0" />
|
||||
<PackageReference Include="RavenDB.DependencyInjection" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
+392
-392
@@ -1,422 +1,422 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Raven.Client;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Raven.Client.Documents;
|
||||
using Raven.Client.Documents.Session;
|
||||
using Raven.Client.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Raven.Client.Documents.Session;
|
||||
using Raven.Client.Exceptions;
|
||||
using Raven.Client.Documents;
|
||||
|
||||
namespace Raven.Identity
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole"></typeparam>
|
||||
public class RoleMan<TRole> : RoleManager<TRole>
|
||||
where TRole : class
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole"></typeparam>
|
||||
public class RoleMan<TRole> : RoleManager<TRole>
|
||||
where TRole : class
|
||||
/// <param name="store"></param>
|
||||
/// <param name="roleValidators"></param>
|
||||
/// <param name="keyNormalizer"></param>
|
||||
/// <param name="errors"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="contextAccessor"></param>
|
||||
public RoleMan(IRoleStore<TRole> store, IEnumerable<IRoleValidator<TRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<TRole>> logger, IHttpContextAccessor contextAccessor) :
|
||||
base(store, roleValidators, keyNormalizer, errors, logger)
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="store"></param>
|
||||
/// <param name="roleValidators"></param>
|
||||
/// <param name="keyNormalizer"></param>
|
||||
/// <param name="errors"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="contextAccessor"></param>
|
||||
public RoleMan(IRoleStore<TRole> store, IEnumerable<IRoleValidator<TRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<TRole>> logger, IHttpContextAccessor contextAccessor) :
|
||||
base(store, roleValidators, keyNormalizer, errors, logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a persistence store for roles.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole">The type of the class representing a role.</typeparam>
|
||||
public class RoleStore<TRole> : RoleStore<TRole, IdentityRoleClaim>
|
||||
where TRole : IdentityRole<IdentityRoleClaim>
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a new instance of <see cref="RoleStore{TRole}"/>.
|
||||
/// </summary>
|
||||
/// <param name="store">The <see cref="IDocumentStore"/>.</param>
|
||||
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
|
||||
public RoleStore(IDocumentStore store, IdentityErrorDescriber describer = null) : base(store, describer) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a entity representing a role claim.
|
||||
/// </summary>
|
||||
/// <param name="role">The associated role.</param>
|
||||
/// <param name="claim">The associated claim.</param>
|
||||
/// <returns>The role claim entity.</returns>
|
||||
protected override IdentityRoleClaim CreateRoleClaim(TRole role, Claim claim)
|
||||
{
|
||||
return new IdentityRoleClaim { ClaimType = claim.Type, ClaimValue = claim.Value };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a persistence store for roles.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole">The type of the class representing a role.</typeparam>
|
||||
/// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
|
||||
public abstract class RoleStore<TRole, TRoleClaim> :
|
||||
IRoleClaimStore<TRole>
|
||||
where TRole : IdentityRole<TRoleClaim>
|
||||
where TRoleClaim : IdentityRoleClaim
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a new instance of <see cref="RoleStore{TRole, TRoleClaim}"/>.
|
||||
/// </summary>
|
||||
/// <param name="store">The <see cref="IDocumentStore"/>.</param>
|
||||
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
|
||||
public RoleStore(IDocumentStore store, IdentityErrorDescriber describer = null)
|
||||
{
|
||||
Store = store ?? throw new ArgumentNullException(nameof(store));
|
||||
ErrorDescriber = describer ?? new IdentityErrorDescriber();
|
||||
}
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the database context for this store.
|
||||
/// </summary>
|
||||
public IDocumentStore Store { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IdentityErrorDescriber"/> for any error that occurred with the current operation.
|
||||
/// </summary>
|
||||
public IdentityErrorDescriber ErrorDescriber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new role in a store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to create in the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(role.Name))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role.Name));
|
||||
}
|
||||
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
var roleId = GetRavenIdFromRoleName(role.Name, Store);
|
||||
await session.StoreAsync(role, roleId);
|
||||
await session.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a persistence store for roles.
|
||||
/// Updates a role in a store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole">The type of the class representing a role.</typeparam>
|
||||
public class RoleStore<TRole> : RoleStore<TRole, IdentityRoleClaim>
|
||||
where TRole : IdentityRole<IdentityRoleClaim>
|
||||
/// <param name="role">The role to update in the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a new instance of <see cref="RoleStore{TRole}"/>.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="IAsyncDocumentSession"/>.</param>
|
||||
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
|
||||
public RoleStore(IAsyncDocumentSession context, IdentityErrorDescriber describer = null) : base(context, describer) { }
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a entity representing a role claim.
|
||||
/// </summary>
|
||||
/// <param name="role">The associated role.</param>
|
||||
/// <param name="claim">The associated claim.</param>
|
||||
/// <returns>The role claim entity.</returns>
|
||||
protected override IdentityRoleClaim CreateRoleClaim(TRole role, Claim claim)
|
||||
// TODO: Assumption is made that TRole entity is being tracked in the current session
|
||||
// If not, then we'll have to Load<TRole> and overwrite all properties except for Id in the loaded entity
|
||||
//role.ConcurrencyStamp = Guid.NewGuid().ToString();
|
||||
try
|
||||
{
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
return new IdentityRoleClaim { ClaimType = claim.Type, ClaimValue = claim.Value };
|
||||
await session.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (ConcurrencyException)
|
||||
{
|
||||
return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure());
|
||||
}
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of a persistence store for roles.
|
||||
/// Deletes a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRole">The type of the class representing a role.</typeparam>
|
||||
/// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
|
||||
public abstract class RoleStore<TRole, TRoleClaim> :
|
||||
IRoleClaimStore<TRole>
|
||||
where TRole : IdentityRole<TRoleClaim>
|
||||
where TRoleClaim : IdentityRoleClaim
|
||||
/// <param name="role">The role to delete from the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a new instance of <see cref="RoleStore{TRole, TRoleClaim}"/>.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="IAsyncDocumentSession"/>.</param>
|
||||
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
|
||||
public RoleStore(IAsyncDocumentSession context, IdentityErrorDescriber describer = null)
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
session.Delete(role.Id);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
AsyncSession = context ?? throw new ArgumentNullException(nameof(context));
|
||||
ErrorDescriber = describer ?? new IdentityErrorDescriber();
|
||||
}
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the database context for this store.
|
||||
/// </summary>
|
||||
public IAsyncDocumentSession AsyncSession { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IdentityErrorDescriber"/> for any error that occurred with the current operation.
|
||||
/// </summary>
|
||||
public IdentityErrorDescriber ErrorDescriber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a flag indicating if changes should be persisted after CreateAsync, UpdateAsync and DeleteAsync are called.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// True if changes should be automatically persisted, otherwise false.
|
||||
/// </value>
|
||||
public bool AutoSaveChanges { get; set; } = true;
|
||||
|
||||
/// <summary>Saves the current store.</summary>
|
||||
/// <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>
|
||||
private async Task SaveChanges(CancellationToken cancellationToken)
|
||||
{
|
||||
if (AutoSaveChanges)
|
||||
{
|
||||
await AsyncSession.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new role in a store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to create in the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(role.Name))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role.Name));
|
||||
}
|
||||
|
||||
var roleId = GetRavenIdFromRoleName(role.Name, AsyncSession.Advanced.DocumentStore);
|
||||
await AsyncSession.StoreAsync(role, roleId);
|
||||
await SaveChanges(cancellationToken);
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a role in a store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to update in the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
// TODO: Assumption is made that TRole entity is being tracked in the current session
|
||||
// If not, then we'll have to Load<TRole> and overwrite all properties except for Id in the loaded entity
|
||||
//role.ConcurrencyStamp = Guid.NewGuid().ToString();
|
||||
try
|
||||
{
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
catch (ConcurrencyException)
|
||||
{
|
||||
return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure());
|
||||
}
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to delete from the store.</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 represents the <see cref="IdentityResult"/> of the asynchronous query.</returns>
|
||||
public async virtual Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
AsyncSession.Delete(role.Id);
|
||||
try
|
||||
{
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
catch (ConcurrencyException)
|
||||
{
|
||||
return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure());
|
||||
}
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ID for a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose ID should be returned.</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 ID of the role.</returns>
|
||||
public Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
return Task.FromResult(role.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose name should be returned.</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 name of the role.</returns>
|
||||
public Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
return Task.FromResult(role.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the name of a role in the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose name should be set.</param>
|
||||
/// <param name="roleName">The name of the role.</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>
|
||||
public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
role.Name = roleName;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the role who has the specified ID as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="id">The role ID to look for.</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 result of the look up.</returns>
|
||||
public virtual Task<TRole> FindByIdAsync(string id, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return AsyncSession.LoadAsync<TRole>(id, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the role who has the specified normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="normalizedName">The normalized role name to look for.</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 result of the look up.</returns>
|
||||
public virtual Task<TRole> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
|
||||
var roleId = GetRavenIdFromRoleName(normalizedName, AsyncSession.Advanced.DocumentStore);
|
||||
return AsyncSession.LoadAsync<TRole>(roleId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a role's normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose normalized name should be retrieved.</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 name of the role.</returns>
|
||||
public virtual Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
return Task.FromResult(role.Name.ToLowerInvariant());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a role's normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose normalized name should be set.</param>
|
||||
/// <param name="normalizedName">The normalized name to set</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>
|
||||
public virtual Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
//role.Name = normalizedName;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws if this class has been disposed.
|
||||
/// </summary>
|
||||
protected void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose the stores
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the claims associated with the specified <paramref name="role"/> as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose claims should be retrieved.</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>
|
||||
public async Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
await Task.FromResult(0);
|
||||
|
||||
return role.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the <paramref name="claim"/> given to the specified <paramref name="role"/>.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to add the claim to.</param>
|
||||
/// <param name="claim">The claim to add to the role.</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>
|
||||
public Task AddClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(claim));
|
||||
}
|
||||
role.Claims.Add(CreateRoleClaim(role, claim));
|
||||
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the <paramref name="claim"/> given from the specified <paramref name="role"/>.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to remove the claim from.</param>
|
||||
/// <param name="claim">The claim to remove from the role.</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>
|
||||
public async Task RemoveClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(claim));
|
||||
}
|
||||
|
||||
await Task.FromResult(0);
|
||||
|
||||
var claims = role.Claims.Where(c => c.ClaimValue == claim.Value).ToList();
|
||||
foreach (var c in claims)
|
||||
{
|
||||
role.Claims.Remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a entity representing a role claim.
|
||||
/// </summary>
|
||||
/// <param name="role">The associated role.</param>
|
||||
/// <param name="claim">The associated claim.</param>
|
||||
/// <returns>The role claim entity.</returns>
|
||||
protected abstract TRoleClaim CreateRoleClaim(TRole role, Claim claim);
|
||||
|
||||
internal static string GetRavenIdFromRoleName(string role, IDocumentStore docStore)
|
||||
{
|
||||
var roleCollection = docStore.Conventions.GetCollectionName(typeof(TRole));
|
||||
var prefix = docStore.Conventions.TransformTypeCollectionNameToDocumentIdPrefix(roleCollection);
|
||||
var partSeparator = docStore.Conventions.IdentityPartsSeparator;
|
||||
return prefix + partSeparator + role;
|
||||
await session.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (ConcurrencyException)
|
||||
{
|
||||
return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure());
|
||||
}
|
||||
return IdentityResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ID for a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose ID should be returned.</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 ID of the role.</returns>
|
||||
public Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
return Task.FromResult(role.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of a role from the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose name should be returned.</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 name of the role.</returns>
|
||||
public Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
return Task.FromResult(role.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the name of a role in the store as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose name should be set.</param>
|
||||
/// <param name="roleName">The name of the role.</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>
|
||||
public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
role.Name = roleName;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the role who has the specified ID as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="id">The role ID to look for.</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 result of the look up.</returns>
|
||||
public virtual Task<TRole> FindByIdAsync(string id, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
return session.LoadAsync<TRole>(id, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the role who has the specified normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="normalizedName">The normalized role name to look for.</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 result of the look up.</returns>
|
||||
public virtual Task<TRole> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
|
||||
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
||||
{
|
||||
var roleId = GetRavenIdFromRoleName(normalizedName, Store);
|
||||
return session.LoadAsync<TRole>(roleId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a role's normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose normalized name should be retrieved.</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 name of the role.</returns>
|
||||
public virtual Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
return Task.FromResult(role.Name.ToLowerInvariant());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a role's normalized name as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose normalized name should be set.</param>
|
||||
/// <param name="normalizedName">The normalized name to set</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>
|
||||
public virtual Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
//role.Name = normalizedName;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws if this class has been disposed.
|
||||
/// </summary>
|
||||
protected void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose the stores
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the claims associated with the specified <paramref name="role"/> as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="role">The role whose claims should be retrieved.</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>
|
||||
public async Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
|
||||
await Task.FromResult(0);
|
||||
|
||||
return role.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the <paramref name="claim"/> given to the specified <paramref name="role"/>.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to add the claim to.</param>
|
||||
/// <param name="claim">The claim to add to the role.</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>
|
||||
public Task AddClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(claim));
|
||||
}
|
||||
role.Claims.Add(CreateRoleClaim(role, claim));
|
||||
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the <paramref name="claim"/> given from the specified <paramref name="role"/>.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to remove the claim from.</param>
|
||||
/// <param name="claim">The claim to remove from the role.</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>
|
||||
public async Task RemoveClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(role));
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(claim));
|
||||
}
|
||||
|
||||
await Task.FromResult(0);
|
||||
|
||||
var claims = role.Claims.Where(c => c.ClaimValue == claim.Value).ToList();
|
||||
foreach (var c in claims)
|
||||
{
|
||||
role.Claims.Remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a entity representing a role claim.
|
||||
/// </summary>
|
||||
/// <param name="role">The associated role.</param>
|
||||
/// <param name="claim">The associated claim.</param>
|
||||
/// <returns>The role claim entity.</returns>
|
||||
protected abstract TRoleClaim CreateRoleClaim(TRole role, Claim claim);
|
||||
|
||||
internal static string GetRavenIdFromRoleName(string role, IDocumentStore docStore)
|
||||
{
|
||||
var roleCollection = docStore.Conventions.GetCollectionName(typeof(TRole));
|
||||
var prefix = docStore.Conventions.TransformTypeCollectionNameToDocumentIdPrefix(roleCollection);
|
||||
var partSeparator = docStore.Conventions.IdentityPartsSeparator;
|
||||
return prefix + partSeparator + role;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Raven.Client;
|
||||
using Raven.Client.Documents;
|
||||
using Raven.Client.Documents.Session;
|
||||
using System;
|
||||
|
||||
namespace Raven.Identity
|
||||
{
|
||||
/// <summary>
|
||||
/// Extends the <see cref="IServiceCollection"/> so that RavenDB services can be registered through it.
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers a RavenDB as the user store.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUser">The type of user. This should be a class you created derived from <see cref="IdentityUser"/>.</typeparam>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="setupAction">Identity options configuration.</param>
|
||||
/// <returns>The identity builder.</returns>
|
||||
public static IdentityBuilder AddRavenDbIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> setupAction = null)
|
||||
where TUser : IdentityUser
|
||||
{
|
||||
return AddRavenDbIdentity<TUser, IdentityRole>(services, setupAction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a RavenDB as the user store.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUser">The type of user. This should be a class you created derived from <see cref="IdentityUser"/>.</typeparam>
|
||||
/// <typeparam name="TRole">The type of role. This should be a class you created derived from <see cref="IdentityRole"/>.</typeparam>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="setupAction">Identity options configuration.</param>
|
||||
/// <returns>The identity builder.</returns>
|
||||
public static IdentityBuilder AddRavenDbIdentity<TUser, TRole>(this IServiceCollection services, Action<IdentityOptions> setupAction = null)
|
||||
where TUser : IdentityUser
|
||||
where TRole : IdentityRole, new()
|
||||
{
|
||||
// Add the AspNet identity system to work with our RavenDB identity objects.
|
||||
IdentityBuilder identityBuilder;
|
||||
if (setupAction != null)
|
||||
{
|
||||
identityBuilder = services.AddIdentity<TUser, TRole>(setupAction)
|
||||
.AddDefaultTokenProviders();
|
||||
}
|
||||
else
|
||||
{
|
||||
identityBuilder = services.AddIdentity<TUser, TRole>()
|
||||
.AddDefaultTokenProviders();
|
||||
}
|
||||
|
||||
services.AddScoped<Microsoft.AspNetCore.Identity.IUserStore<TUser>, UserStore<TUser, TRole>>();
|
||||
services.AddScoped<Microsoft.AspNetCore.Identity.IRoleStore<TRole>, RoleStore<TRole>>();
|
||||
|
||||
return identityBuilder;
|
||||
}
|
||||
}
|
||||
}
|
||||
+855
-868
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
|
||||
<PackageReference Include="RavenDB.Client" Version="4.2.0" />
|
||||
<PackageReference Include="RavenDB.DependencyInjection" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
@@ -11,6 +11,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||
<PackageReference Include="RavenDB.Client" Version="4.2.0" />
|
||||
<PackageReference Include="RavenDB.DependencyInjection" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user