From b85b2ad8466e1c772f48ef81de427f0da713c80f Mon Sep 17 00:00:00 2001 From: Judah Himango CW Date: Tue, 25 Jun 2019 18:27:24 -0500 Subject: [PATCH] Fixed bug in MVC sample. --- RavenDB.Identity/RoleStore.cs | 18 +++++++++++++++--- RavenDB.Identity/UserStore.cs | 6 +++--- Samples/Mvc/Controllers/RavenController.cs | 2 +- Samples/Mvc/Views/Account/SignIn.cshtml | 3 ++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/RavenDB.Identity/RoleStore.cs b/RavenDB.Identity/RoleStore.cs index 0987dd9..46ef498 100644 --- a/RavenDB.Identity/RoleStore.cs +++ b/RavenDB.Identity/RoleStore.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Raven.Client.Documents.Session; using Raven.Client.Exceptions; +using Raven.Client.Documents; namespace Raven.Identity { @@ -133,7 +134,9 @@ namespace Raven.Identity { throw new ArgumentNullException(nameof(role.Name)); } - await AsyncSession.StoreAsync(role, $"IdentityRoles/{role.Name}"); + + var roleId = GetRavenIdFromRoleName(role.Name, AsyncSession.Advanced.DocumentStore); + await AsyncSession.StoreAsync(role, roleId); await SaveChanges(cancellationToken); return IdentityResult.Success; } @@ -269,7 +272,9 @@ namespace Raven.Identity { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); - return AsyncSession.LoadAsync($"IdentityRoles/{normalizedName.ToLower()}"); + + var roleId = GetRavenIdFromRoleName(normalizedName, AsyncSession.Advanced.DocumentStore); + return AsyncSession.LoadAsync(roleId); } /// @@ -286,7 +291,8 @@ namespace Raven.Identity { throw new ArgumentNullException(nameof(role)); } - return Task.FromResult(role.Name.ToLower()); + + return Task.FromResult(role.Name.ToLowerInvariant()); } /// @@ -404,5 +410,11 @@ namespace Raven.Identity /// The associated claim. /// The role claim entity. protected abstract TRoleClaim CreateRoleClaim(TRole role, Claim claim); + + internal static string GetRavenIdFromRoleName(string role, IDocumentStore docStore) + { + var partSeparator = docStore.Conventions.IdentityPartsSeparator; + return "IdentityRole" + partSeparator + role; + } } } diff --git a/RavenDB.Identity/UserStore.cs b/RavenDB.Identity/UserStore.cs index 0d1daa9..f5d0043 100644 --- a/RavenDB.Identity/UserStore.cs +++ b/RavenDB.Identity/UserStore.cs @@ -387,8 +387,8 @@ namespace Raven.Identity public async Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken) { ThrowIfNullDisposedCancelled(user, cancellationToken); - - var roleNameLowered = roleName.ToLower(); + + var roleNameLowered = roleName.ToLowerInvariant(); if (!user.Roles.Contains(roleNameLowered, StringComparer.OrdinalIgnoreCase)) { user.GetRolesList().Add(roleNameLowered); @@ -419,7 +419,7 @@ namespace Raven.Identity user.GetRolesList().RemoveAll(r => string.Equals(r, roleName, StringComparison.OrdinalIgnoreCase)); - var roleId = "IdentityRoles/" + roleName.ToLower(); + var roleId = RoleStore.GetRavenIdFromRoleName(roleName, DbSession.Advanced.DocumentStore); var roleOrNull = await DbSession.LoadAsync(roleId, cancellationToken); if (roleOrNull != null) { diff --git a/Samples/Mvc/Controllers/RavenController.cs b/Samples/Mvc/Controllers/RavenController.cs index e5c7c61..76b0391 100644 --- a/Samples/Mvc/Controllers/RavenController.cs +++ b/Samples/Mvc/Controllers/RavenController.cs @@ -15,7 +15,7 @@ namespace Sample.Mvc.Controllers { public RavenController(IAsyncDocumentSession dbSession) { - this.DbSession = DbSession; + this.DbSession = dbSession; // RavenDB best practice: during save, wait for the indexes to update. // This way, Post-Redirect-Get scenarios won't be affected by stale indexes. diff --git a/Samples/Mvc/Views/Account/SignIn.cshtml b/Samples/Mvc/Views/Account/SignIn.cshtml index b044a93..6f2219f 100644 --- a/Samples/Mvc/Views/Account/SignIn.cshtml +++ b/Samples/Mvc/Views/Account/SignIn.cshtml @@ -2,7 +2,8 @@ ViewData["Title"] = "Sign in"; } -

Hi! Please sign in. (Not registered? Register here.)

+

Hi! Please sign in

+

Not registered? Register here.