Skip to content

Instantly share code, notes, and snippets.

@artkpv
Created June 29, 2015 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artkpv/b9228f5a27c54b061f90 to your computer and use it in GitHub Desktop.
Save artkpv/b9228f5a27c54b061f90 to your computer and use it in GitHub Desktop.
Isolating AspNet.Identity.EntityFramework
/*
* That adapter allows to isolate AspNet.Identity.EntityFramework
* assembly with its IdentityUser and other classes from
* AspNet.Identity.Core. It works with ApplicationUser that is not
* derived from IdentityUser.
* Author: w1ld at inbox dot ru
* Created: 2015 06 29
*/
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace SOMENAMESPACE
{
// sse https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs
internal class UserStoreAdapter : IUserStore<ApplicationUser>,
IUserPasswordStore<ApplicationUser>,
IUserRoleStore<ApplicationUser>,
IUserEmailStore<ApplicationUser>,
IUserLoginStore<ApplicationUser>,
IUserSecurityStampStore<ApplicationUser>
// IUserClaimStore<ApplicationUser>,
// IUserLockoutStore<ApplicationUser, string>
{
private readonly YourDbContext _context;
private readonly UserStore<EfApplicationUser> _userStore;
public UserStoreAdapter(YourDbContext context)
{
_context = context;
_userStore = new UserStore<EfApplicationUser>(context);
_userStore.AutoSaveChanges = false; // Beware to save changes by yourselves
}
public async Task SetEmailAsync(ApplicationUser user, string email)
{
var efApplicationUser = GetMapped(user);
await _userStore.SetEmailAsync(efApplicationUser, email);
Mapper.Map(efApplicationUser, user);
}
public async Task<string> GetEmailAsync(ApplicationUser user)
{
return await _userStore.GetEmailAsync(GetMapped(user));
}
public async Task<bool> GetEmailConfirmedAsync(ApplicationUser user)
{
return await _userStore.GetEmailConfirmedAsync(GetMapped(user));
}
public async Task SetEmailConfirmedAsync(ApplicationUser user, bool confirmed)
{
var efApplicationUser = GetMapped(user);
await _userStore.SetEmailConfirmedAsync(efApplicationUser, confirmed);
Mapper.Map(efApplicationUser, user);
}
public async Task<ApplicationUser> FindByEmailAsync(string email)
{
var efApplicationUser = await _userStore.FindByEmailAsync(email);
return Mapper.Map<EfApplicationUser, ApplicationUser>(efApplicationUser);
}
public async Task AddLoginAsync(ApplicationUser user, UserLoginInfo login)
{
await _userStore.AddLoginAsync(GetMapped(user), login);
}
public async Task RemoveLoginAsync(ApplicationUser user, UserLoginInfo login)
{
await _userStore.RemoveLoginAsync(GetMapped(user), login);
}
public async Task<IList<UserLoginInfo>> GetLoginsAsync(ApplicationUser user)
{
return await _userStore.GetLoginsAsync(GetMapped(user));
}
public async Task<ApplicationUser> FindAsync(UserLoginInfo login)
{
var found = await _userStore.FindAsync(login);
return Mapper.Map<EfApplicationUser, ApplicationUser>(found);
}
public async Task SetPasswordHashAsync(ApplicationUser user, string passwordHash)
{
var mapped = GetMapped(user);
await _userStore.SetPasswordHashAsync(mapped, passwordHash);
Mapper.Map(mapped, user);
Contract.Assert(mapped.PasswordHash == user.PasswordHash);
}
public async Task<string> GetPasswordHashAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
return await _userStore.GetPasswordHashAsync(mapped);
}
public async Task<bool> HasPasswordAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
return await _userStore.HasPasswordAsync(mapped);
}
public async Task AddToRoleAsync(ApplicationUser user, string roleName)
{
var mapped = GetMapped(user);
await _userStore.AddToRoleAsync(mapped, roleName);
}
public async Task RemoveFromRoleAsync(ApplicationUser user, string roleName)
{
var mapped = GetMapped(user);
await _userStore.RemoveFromRoleAsync(mapped, roleName);
}
public async Task<IList<string>> GetRolesAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
return await _userStore.GetRolesAsync(mapped);
}
public async Task<bool> IsInRoleAsync(ApplicationUser user, string roleName)
{
var mapped = GetMapped(user);
return await _userStore.IsInRoleAsync(mapped, roleName);
}
public async Task SetSecurityStampAsync(ApplicationUser user, string stamp)
{
var efApplicationUser = GetMapped(user);
await _userStore.SetSecurityStampAsync(efApplicationUser, stamp);
Mapper.Map(efApplicationUser, user);
Contract.Assert(user.SecurityStamp == stamp);
}
public async Task<string> GetSecurityStampAsync(ApplicationUser user)
{
return await _userStore.GetSecurityStampAsync(GetMapped(user));
}
public async Task CreateAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
await _userStore.CreateAsync(mapped);
Mapper.Map(mapped, user);
Contract.Assert(user.SecurityStamp == mapped.SecurityStamp);
}
public async Task UpdateAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
await _userStore.UpdateAsync(mapped);
Mapper.Map(mapped, user);
Contract.Assert(mapped.SecurityStamp == user.SecurityStamp);
}
public async Task DeleteAsync(ApplicationUser user)
{
var mapped = GetMapped(user);
await _userStore.DeleteAsync(mapped);
}
public async Task<ApplicationUser> FindByIdAsync(string userId)
{
var found = await _userStore.FindByIdAsync(userId);
return Mapper.Map<EfApplicationUser, ApplicationUser>(found);
}
public async Task<ApplicationUser> FindByNameAsync(string userName)
{
var found = await _userStore.FindByNameAsync(userName);
return Mapper.Map<EfApplicationUser, ApplicationUser>(found);
}
private EfApplicationUser GetMapped(ApplicationUser user)
{
// use EF cache:
return Mapper.Map(user, _context.Users.Local.FirstOrDefault(u => u.Id == user.Id) ?? new EfApplicationUser());
}
public void Dispose()
{
_userStore.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment