Skip to content

Instantly share code, notes, and snippets.

@runceel
Created August 20, 2015 08:29
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 runceel/00a2af37265184087fbb to your computer and use it in GitHub Desktop.
Save runceel/00a2af37265184087fbb to your computer and use it in GitHub Desktop.
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ASPNETIdentityRole.Models
{
public class ApplicationUser : IUser<string>
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string UserName { get; set; }
public string Password { get; set; }
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
{
}
}
public class ApplicationRole : IRole<string>
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
}
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
}
}
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(
UserManager<ApplicationUser, string> userManager,
IAuthenticationManager authenticationManager) : base(userManager, authenticationManager)
{
}
}
public class UserStore :
IUserStore<ApplicationUser>,
IUserStore<ApplicationUser, string>,
IUserPasswordStore<ApplicationUser, string>,
IUserRoleStore<ApplicationUser, string>
{
private static List<ApplicationUser> Users { get; } = new List<ApplicationUser>();
private static List<ApplicationRole> Roles { get; } = new List<ApplicationRole>();
private static List<Tuple<string, string>> UserRoleMap { get; } = new List<Tuple<string, string>>();
public Task AddToRoleAsync(ApplicationUser user, string roleName)
{
var role = Roles.FirstOrDefault(x => x.Name == roleName);
if (role == null) { throw new InvalidOperationException(); }
var userRoleMap = UserRoleMap.FirstOrDefault(x => x.Item1 == user.Id && x.Item2 == role.Id);
if (userRoleMap == null)
{
UserRoleMap.Add(Tuple.Create(user.Id, role.Id));
}
return Task.FromResult(default(object));
}
public Task CreateAsync(ApplicationUser user)
{
Users.Add(user);
return Task.FromResult(default(object));
}
public Task DeleteAsync(ApplicationUser user)
{
Users.Remove(Users.First(x => x.Id == user.Id));
return Task.FromResult(default(object));
}
public void Dispose()
{
}
public Task<ApplicationUser> FindByIdAsync(string userId)
{
var result = Users.FirstOrDefault(x => x.Id == userId);
if (result == null) { throw new InvalidOperationException(); }
return Task.FromResult(result);
}
public Task<ApplicationUser> FindByNameAsync(string userName)
{
var result = Users.FirstOrDefault(x => x.UserName == userName);
if (result == null) { throw new InvalidOperationException(); }
return Task.FromResult(result);
}
public Task<string> GetPasswordHashAsync(ApplicationUser user)
{
return Task.FromResult(user.Password);
}
public Task<IList<string>> GetRolesAsync(ApplicationUser user)
{
IList<string> roleNames = UserRoleMap.Where(x => x.Item1 == user.Id).Select(x => x.Item2)
.Select(x => Roles.First(y => y.Id == x))
.Select(x => x.Name)
.ToList();
return Task.FromResult(roleNames);
}
public Task<bool> HasPasswordAsync(ApplicationUser user)
{
return Task.FromResult(user.Password != null);
}
public async Task<bool> IsInRoleAsync(ApplicationUser user, string roleName)
{
var roles = await this.GetRolesAsync(user);
return roles.FirstOrDefault(x => x.ToUpper() == roleName.ToUpper()) != null;
}
public Task RemoveFromRoleAsync(ApplicationUser user, string roleName)
{
var role = Roles.FirstOrDefault(x => x.Name == roleName);
if (role == null) { throw new InvalidOperationException(); }
var userRoleMap = UserRoleMap.FirstOrDefault(x => x.Item1 == user.Id && x.Item2 == role.Id);
if (userRoleMap != null)
{
UserRoleMap.Remove(userRoleMap);
}
return Task.FromResult(default(object));
}
public Task SetPasswordHashAsync(ApplicationUser user, string passwordHash)
{
user.Password = passwordHash;
return Task.FromResult(default(object));
}
public Task UpdateAsync(ApplicationUser user)
{
var r = Users.FirstOrDefault(x => x.Id == user.Id);
if (r == null) { throw new InvalidOperationException(); }
r.UserName = user.UserName;
r.Password = user.Password;
return Task.FromResult(default(object));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment