Skip to content

Instantly share code, notes, and snippets.

@weitzhandler
Last active July 5, 2017 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weitzhandler/9f5871e82d201f45bbcd3d3ab29bafc8 to your computer and use it in GitHub Desktop.
Save weitzhandler/9f5871e82d201f45bbcd3d3ab29bafc8 to your computer and use it in GitHub Desktop.
Microsoft.AspNetCore.Identity.EntityFramework
using System;
using System.Data.Entity;
using System.Threading;
using System.Threading.Tasks;
using static Microsoft.AspNetCore.Identity.EntityFramework.BaseMethodWrapper;
namespace Microsoft.AspNetCore.Identity.EntityFramework
{
public interface IKeyConverter<TKey>
{
TKey Convert(string key);
}
public class Int32KeyConverter : IKeyConverter<int>
{
public int Convert(string key) => int.Parse(key);
}
public class UserStore<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim, TKeyConverter>
: AspNet.Identity.EntityFramework.UserStore<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>,
IUserStore<TUser>,
IUserPasswordStore<TUser>
where TUser : AspNet.Identity.EntityFramework.IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : AspNet.Identity.EntityFramework.IdentityRole<TKey, TUserRole>
where TKey : IEquatable<TKey>
where TUserLogin : AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>, new()
where TUserRole : AspNet.Identity.EntityFramework.IdentityUserRole<TKey>, new()
where TUserClaim : AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>, new()
where TKeyConverter : IKeyConverter<TKey>, new()
{
private bool _disposed;
TKeyConverter converter = new TKeyConverter();
public UserStore(DbContext context) : base(context)
{
}
public virtual async Task<IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) =>
await ExecuteAsync(() => CreateAsync(user), cancellationToken);
public virtual async Task<IdentityResult> DeleteAsync(TUser user, CancellationToken cancellationToken) =>
await ExecuteAsync(() => DeleteAsync(user), cancellationToken);
public virtual async Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var key = converter.Convert(userId);
return await FindByIdAsync(key);
}
public virtual async Task<TUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return await FindByNameAsync(normalizedUserName);
}
public virtual Task<string> GetNormalizedUserNameAsync(TUser user, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(user.UserName);
}
public virtual Task<string> GetUserIdAsync(TUser user, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(user.Id.ToString());
}
public virtual Task<string> GetUserNameAsync(TUser user, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(user.UserName);
}
public virtual Task SetNormalizedUserNameAsync(TUser user, string normalizedName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
user.UserName = normalizedName;
return Task.CompletedTask;
}
public virtual Task SetUserNameAsync(TUser user, string userName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
user.UserName = userName;
return Task.CompletedTask;
}
public virtual async Task<IdentityResult> UpdateAsync(TUser user, CancellationToken cancellationToken) =>
await ExecuteAsync(() => UpdateAsync(user), cancellationToken);
public Task SetPasswordHashAsync(TUser user, string passwordHash, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return SetPasswordHashAsync(user, passwordHash);
}
public Task<string> GetPasswordHashAsync(TUser user, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return GetPasswordHashAsync(user);
}
public Task<bool> HasPasswordAsync(TUser user, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return HasPasswordAsync(user);
}
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
}
protected override void Dispose(bool disposing)
{
_disposed = true;
base.Dispose(disposing);
}
}
public class RoleStore<TRole, TKey, TUserRole, TKeyConverter>
: AspNet.Identity.EntityFramework.RoleStore<TRole, TKey, TUserRole>, IRoleStore<TRole>
where TRole : AspNet.Identity.EntityFramework.IdentityRole<TKey, TUserRole>, new()
where TUserRole : AspNet.Identity.EntityFramework.IdentityUserRole<TKey>, new()
where TKeyConverter : IKeyConverter<TKey>, new()
{
TKeyConverter converter = new TKeyConverter();
public RoleStore(DbContext context) : base(context)
{
}
public virtual async Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken) =>
await ExecuteAsync(() => CreateAsync(role), cancellationToken);
public virtual async Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken) =>
await ExecuteAsync(() => DeleteAsync(role), cancellationToken);
public virtual async Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var key = converter.Convert(roleId);
return await FindByIdAsync(key);
}
public virtual async Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return await FindByNameAsync(normalizedRoleName);
}
public virtual Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(role.Name);
}
public virtual Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(role.Id.ToString());
}
public virtual Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Task.FromResult(role.Name);
}
public virtual Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
role.Name = normalizedName;
return Task.CompletedTask;
}
public virtual Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
role.Name = roleName;
return Task.CompletedTask;
}
public virtual async Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken)
{
return await ExecuteAsync(() => UpdateAsync(role), cancellationToken);
}
bool _disposed;
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
}
protected override void Dispose(bool disposing)
{
_disposed = true;
base.Dispose(disposing);
}
}
internal static class BaseMethodWrapper
{
public async static Task<IdentityResult> ExecuteAsync(Func<Task> method, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await method();
return IdentityResult.Success;
}
catch (Exception e)
{
return IdentityResult.Failed(new IdentityError { Code = $"{e.HResult}{e.GetType()}", Description = $"{e.Message}\n{new string('*', 10)}\n{e.StackTrace}" });
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment