Initial Identity.Entity implementation
- Most child entities related apis not working/implemented - Unit tests disabled in kbuild for now Code review fix
This commit is contained in:
parent
8549d83b7c
commit
041db7cb69
|
|
@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Identity.S
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Identity.Security.Test.net45", "test\Microsoft.AspNet.Identity.Security.Test\Microsoft.AspNet.Identity.Security.Test.net45.csproj", "{30A2C4BB-86AB-4971-BC63-4761E8CE7D0F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Identity.Entity.Test.net45", "test\Microsoft.AspNet.Identity.Entity.Test\Microsoft.AspNet.Identity.Entity.Test.net45.csproj", "{515A8ACE-9359-474D-B4AE-E7F148CDDB39}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -83,6 +85,10 @@ Global
|
|||
{30A2C4BB-86AB-4971-BC63-4761E8CE7D0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{30A2C4BB-86AB-4971-BC63-4761E8CE7D0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{30A2C4BB-86AB-4971-BC63-4761E8CE7D0F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{515A8ACE-9359-474D-B4AE-E7F148CDDB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{515A8ACE-9359-474D-B4AE-E7F148CDDB39}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{515A8ACE-9359-474D-B4AE-E7F148CDDB39}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{515A8ACE-9359-474D-B4AE-E7F148CDDB39}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -93,6 +99,7 @@ Global
|
|||
{E00E23B0-79B8-41E1-9998-57FECA1F2535} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E}
|
||||
{9102E676-B509-4A78-AD66-A479C50FD1C3} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E}
|
||||
{30A2C4BB-86AB-4971-BC63-4761E8CE7D0F} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E}
|
||||
{515A8ACE-9359-474D-B4AE-E7F148CDDB39} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E}
|
||||
{B72401D7-47F6-4A98-89D5-CCBFEFC5B2B8} = {F6B0C0E9-C346-49D0-B583-95B6CE04BB1B}
|
||||
{E52361C9-1F0B-4229-86A0-E5C7C12A5429} = {F6B0C0E9-C346-49D0-B583-95B6CE04BB1B}
|
||||
{054B3FFA-7196-466F-9A8A-593FFE037A69} = {F6B0C0E9-C346-49D0-B583-95B6CE04BB1B}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Data.InMemory;
|
||||
using Microsoft.Data.SqlServer;
|
||||
using Microsoft.Data.Entity.Metadata;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class IdentityContext :
|
||||
IdentityContext<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
|
||||
{
|
||||
public IdentityContext(EntityConfiguration config) : base(config) { }
|
||||
public IdentityContext() { }
|
||||
}
|
||||
|
||||
public class IdentityContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : EntityContext
|
||||
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
|
||||
where TRole : IdentityRole<TKey, TUserRole> /*, TUserRole*/
|
||||
where TUserLogin : IdentityUserLogin<TKey>
|
||||
where TUserRole : IdentityUserRole<TKey>
|
||||
where TUserClaim : IdentityUserClaim<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
|
||||
public EntitySet<TUser> Users { get; set; }
|
||||
public EntitySet<TRole> Roles { get; set; }
|
||||
|
||||
public IdentityContext() { }
|
||||
public IdentityContext(EntityConfiguration config) : base(config) { }
|
||||
|
||||
protected override void OnConfiguring(EntityConfigurationBuilder builder)
|
||||
{
|
||||
//#if NET45
|
||||
// builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=IdentityDb;Trusted_Connection=True;");
|
||||
//#else
|
||||
builder.UseDataStore(new InMemoryDataStore());
|
||||
//#endif
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<TUser>()
|
||||
.Key(u => u.Id)
|
||||
.Properties(ps => ps.Property(u => u.UserName));
|
||||
//.ToTable("AspNetUsers");
|
||||
builder.Entity<TRole>()
|
||||
.Key(r => r.Id);
|
||||
//.ToTable("AspNetRoles");
|
||||
|
||||
builder.Entity<TUserRole>()
|
||||
.Key(r => new {r.UserId, r.RoleId})
|
||||
.ForeignKeys(fk => fk.ForeignKey<TUser>(f => f.UserId))
|
||||
.ForeignKeys(fk => fk.ForeignKey<TRole>(f => f.RoleId));
|
||||
//.ToTable("AspNetUserRoles");
|
||||
|
||||
builder.Entity<TUserLogin>()
|
||||
.Key(l => new {l.LoginProvider, l.ProviderKey, l.UserId})
|
||||
.ForeignKeys(fk => fk.ForeignKey<TUser>(f => f.UserId));
|
||||
//.ToTable("AspNetUserLogins");
|
||||
|
||||
builder.Entity<TUserClaim>()
|
||||
.Key(c => c.Id)
|
||||
.ForeignKeys(fk => fk.ForeignKey<TUser>(f => f.UserId));
|
||||
//.ToTable("AspNetUserClaims");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a Role entity
|
||||
/// </summary>
|
||||
public class IdentityRole : IdentityRole<string, IdentityUserRole>
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public IdentityRole()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="roleName"></param>
|
||||
public IdentityRole(string roleName)
|
||||
: this()
|
||||
{
|
||||
Name = roleName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Role entity
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TUserRole"></typeparam>
|
||||
public class IdentityRole<TKey, TUserRole> : IRole<TKey>
|
||||
where TUserRole : IdentityUserRole<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public IdentityRole()
|
||||
{
|
||||
Users = new List<TUserRole>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Navigation property for users in the role
|
||||
/// </summary>
|
||||
public virtual ICollection<TUserRole> Users { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Role id
|
||||
/// </summary>
|
||||
public virtual TKey Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Role name
|
||||
/// </summary>
|
||||
public virtual string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
|
||||
{
|
||||
public IdentityUser()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
public IdentityUser(string userName) : this()
|
||||
{
|
||||
UserName = userName;
|
||||
}
|
||||
}
|
||||
|
||||
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
|
||||
where TLogin : IdentityUserLogin<TKey>
|
||||
where TRole : IdentityUserRole<TKey>
|
||||
where TClaim : IdentityUserClaim<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public IdentityUser()
|
||||
{
|
||||
Claims = new List<TClaim>();
|
||||
Roles = new List<TRole>();
|
||||
Logins = new List<TLogin>();
|
||||
|
||||
}
|
||||
|
||||
public virtual TKey Id { get; set; }
|
||||
public virtual string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Email
|
||||
/// </summary>
|
||||
public virtual string Email { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if the email is confirmed, default is false
|
||||
/// </summary>
|
||||
public virtual bool EmailConfirmed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The salted/hashed form of the user password
|
||||
/// </summary>
|
||||
public virtual string PasswordHash { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A random value that should change whenever a users credentials have changed (password changed, login removed)
|
||||
/// </summary>
|
||||
public virtual string SecurityStamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PhoneNumber for the user
|
||||
/// </summary>
|
||||
public virtual string PhoneNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if the phone number is confirmed, default is false
|
||||
/// </summary>
|
||||
public virtual bool PhoneNumberConfirmed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Is two factor enabled for the user
|
||||
/// </summary>
|
||||
public virtual bool TwoFactorEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// DateTime in UTC when lockout ends, any time in the past is considered not locked out.
|
||||
/// </summary>
|
||||
public virtual DateTime? LockoutEndDateUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Is lockout enabled for this user
|
||||
/// </summary>
|
||||
public virtual bool LockoutEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Used to record failures for the purposes of lockout
|
||||
/// </summary>
|
||||
public virtual int AccessFailedCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Navigation property for user roles
|
||||
/// </summary>
|
||||
public virtual ICollection<TRole> Roles { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Navigation property for user claims
|
||||
/// </summary>
|
||||
public virtual ICollection<TClaim> Claims { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Navigation property for user logins
|
||||
/// </summary>
|
||||
public virtual ICollection<TLogin> Logins { get; private set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class IdentityUserClaim : IdentityUserClaim<string> { }
|
||||
|
||||
/// <summary>
|
||||
/// EntityType that represents one specific user claim
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
public class IdentityUserClaim<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// Primary key
|
||||
/// </summary>
|
||||
public virtual int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// User Id for the user who owns this login
|
||||
/// </summary>
|
||||
public virtual TKey UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Claim type
|
||||
/// </summary>
|
||||
public virtual string ClaimType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Claim value
|
||||
/// </summary>
|
||||
public virtual string ClaimValue { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class IdentityUserLogin : IdentityUserLogin<string> { }
|
||||
|
||||
/// <summary>
|
||||
/// Entity type for a user's login (i.e. facebook, google)
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
public class IdentityUserLogin<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// The login provider for the login (i.e. facebook, google)
|
||||
/// </summary>
|
||||
public virtual string LoginProvider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Key representing the login for the provider
|
||||
/// </summary>
|
||||
public virtual string ProviderKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// User Id for the user who owns this login
|
||||
/// </summary>
|
||||
public virtual TKey UserId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class IdentityUserRole : IdentityUserRole<string> { }
|
||||
|
||||
/// <summary>
|
||||
/// EntityType that represents a user belonging to a role
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
public class IdentityUserRole<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// UserId for the user that is in the role
|
||||
/// </summary>
|
||||
public virtual TKey UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// RoleId for the role
|
||||
/// </summary>
|
||||
public virtual TKey RoleId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.Entity;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class RoleStore<TRole, TKey> :
|
||||
IQueryableRoleStore<TRole, TKey>
|
||||
where TRole : class,IRole<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
private bool _disposed;
|
||||
|
||||
public RoleStore(EntityContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException("context");
|
||||
}
|
||||
Context = context;
|
||||
AutoSaveChanges = true;
|
||||
}
|
||||
|
||||
public EntityContext Context { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// If true will call SaveChanges after Create/Update/Delete
|
||||
/// </summary>
|
||||
public bool AutoSaveChanges { get; set; }
|
||||
|
||||
private async Task SaveChanges(CancellationToken cancellationToken)
|
||||
{
|
||||
if (AutoSaveChanges)
|
||||
{
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task<TRole> GetRoleAggregate(Expression<Func<TRole, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return Roles.SingleOrDefaultAsync(filter, cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task Create(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException("role");
|
||||
}
|
||||
await Context.AddAsync(role, cancellationToken);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task Update(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException("role");
|
||||
}
|
||||
await Context.UpdateAsync(role, cancellationToken);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task Delete(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentNullException("role");
|
||||
}
|
||||
Context.Delete(role);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a role by id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<TRole> FindById(TKey id, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return Roles.SingleOrDefaultAsync(r => r.Id.Equals(id), cancellationToken);
|
||||
//return GetRoleAggregate(u => u.Id.Equals(id));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a role by name
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<TRole> FindByName(string name, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == name.ToUpper(), cancellationToken);
|
||||
//return GetRoleAggregate(u => u.Name.ToUpper() == name.ToUpper());
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose the store
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public IQueryable<TRole> Roles
|
||||
{
|
||||
get { return Context.Set<TRole>(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,797 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.Entity;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity
|
||||
{
|
||||
public class UserStore :
|
||||
UserStore<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
|
||||
{
|
||||
public UserStore(EntityContext context) : base(context) { }
|
||||
}
|
||||
|
||||
public class UserStore<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> :
|
||||
IUserLoginStore<TUser, TKey>,
|
||||
IUserClaimStore<TUser, TKey>,
|
||||
IUserRoleStore<TUser, TKey>,
|
||||
IUserPasswordStore<TUser, TKey>,
|
||||
IUserSecurityStampStore<TUser, TKey>,
|
||||
IQueryableUserStore<TUser, TKey>,
|
||||
IUserEmailStore<TUser, TKey>,
|
||||
IUserPhoneNumberStore<TUser, TKey>,
|
||||
IUserTwoFactorStore<TUser, TKey>,
|
||||
IUserLockoutStore<TUser, TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
|
||||
where TRole : IdentityRole<TKey, TUserRole>
|
||||
where TUserLogin : IdentityUserLogin<TKey>, new()
|
||||
where TUserRole : IdentityUserRole<TKey>, new()
|
||||
where TUserClaim : IdentityUserClaim<TKey>, new()
|
||||
{
|
||||
private bool _disposed;
|
||||
|
||||
public UserStore(EntityContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException("context");
|
||||
}
|
||||
Context = context;
|
||||
AutoSaveChanges = true;
|
||||
}
|
||||
|
||||
public EntityContext Context { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// If true will call SaveChanges after Create/Update/Delete
|
||||
/// </summary>
|
||||
public bool AutoSaveChanges { get; set; }
|
||||
|
||||
private Task SaveChanges(CancellationToken cancellationToken)
|
||||
{
|
||||
return AutoSaveChanges ? Context.SaveChangesAsync(cancellationToken) : Task.FromResult(0);
|
||||
}
|
||||
|
||||
protected virtual Task<TUser> GetUserAggregate(Expression<Func<TUser, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return Users.SingleOrDefaultAsync(filter, cancellationToken);
|
||||
//Include(u => u.Roles)
|
||||
//.Include(u => u.Claims)
|
||||
//.Include(u => u.Logins)
|
||||
}
|
||||
|
||||
public async virtual Task Create(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
await Context.AddAsync(user, cancellationToken);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task Update(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
await Context.UpdateAsync(user, cancellationToken);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task Delete(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
Context.Delete(user);
|
||||
await SaveChanges(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a user by id
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<TUser> FindById(TKey userId, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return Users.SingleOrDefaultAsync(u => u.Id.Equals(userId), cancellationToken);
|
||||
// TODO: return GetUserAggregate(u => u.Id.Equals(userId), cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a user by name
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<TUser> FindByName(string userName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return Users.SingleOrDefaultAsync(u => u.UserName.ToUpper() == userName.ToUpper(), cancellationToken);
|
||||
// TODO: return GetUserAggregate(u => u.UserName.ToUpper() == userName.ToUpper(), cancellationToken);
|
||||
}
|
||||
|
||||
public IQueryable<TUser> Users
|
||||
{
|
||||
get { return Context.Set<TUser>(); }
|
||||
}
|
||||
|
||||
public async virtual Task AddLogin(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
if (login == null)
|
||||
{
|
||||
throw new ArgumentNullException("login");
|
||||
}
|
||||
var l = new TUserLogin
|
||||
{
|
||||
UserId = user.Id,
|
||||
ProviderKey = login.ProviderKey,
|
||||
LoginProvider = login.LoginProvider
|
||||
};
|
||||
await Context.Set<TUserLogin>().AddAsync(l, cancellationToken);
|
||||
user.Logins.Add(l);
|
||||
}
|
||||
|
||||
public virtual Task RemoveLogin(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual Task<IList<UserLoginInfo>> GetLogins(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
IList<UserLoginInfo> result =
|
||||
user.Logins.Select(l => new UserLoginInfo(l.LoginProvider, l.ProviderKey)).ToList();
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
public async virtual Task<TUser> Find(UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (login == null)
|
||||
{
|
||||
throw new ArgumentNullException("login");
|
||||
}
|
||||
var provider = login.LoginProvider;
|
||||
var key = login.ProviderKey;
|
||||
// TODO: use FirstOrDefaultAsync
|
||||
var userLogin =
|
||||
Context.Set<TUserLogin>().FirstOrDefault(l => l.LoginProvider == provider && l.ProviderKey == key);
|
||||
if (userLogin != null)
|
||||
{
|
||||
return await GetUserAggregate(u => u.Id.Equals(userLogin.UserId), cancellationToken);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the password hash for a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="passwordHash"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetPasswordHash(TUser user, string passwordHash, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.PasswordHash = passwordHash;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the password hash for a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<string> GetPasswordHash(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.PasswordHash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the user has a password set
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> HasPassword(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return Task.FromResult(user.PasswordHash != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the claims for a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<IList<Claim>> GetClaims(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
IList<Claim> result = user.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a claim to a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="claim"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task AddClaim(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException("claim");
|
||||
}
|
||||
user.Claims.Add(new TUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a claim from a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="claim"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task RemoveClaim(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
if (claim == null)
|
||||
{
|
||||
throw new ArgumentNullException("claim");
|
||||
}
|
||||
var claims =
|
||||
user.Claims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList();
|
||||
foreach (var c in claims)
|
||||
{
|
||||
user.Claims.Remove(c);
|
||||
}
|
||||
// TODO:these claims might not exist in the dbset
|
||||
//var query =
|
||||
// _userClaims.Where(
|
||||
// uc => uc.UserId.Equals(user.Id) && uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type);
|
||||
//foreach (var c in query)
|
||||
//{
|
||||
// _userClaims.Remove(c);
|
||||
//}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the user email is confirmed
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> GetEmailConfirmed(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.EmailConfirmed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set IsConfirmed on the user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="confirmed"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetEmailConfirmed(TUser user, bool confirmed, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.EmailConfirmed = confirmed;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the user email
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetEmail(TUser user, string email, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.Email = email;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the user's email
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<string> GetEmail(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.Email);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a user by email
|
||||
/// </summary>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<TUser> FindByEmail(string email, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
return Task.FromResult(Users.SingleOrDefault(u => u.Email.ToUpper() == email.ToUpper()));
|
||||
//return GetUserAggregate(u => u.Email.ToUpper() == email.ToUpper(), cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the DateTimeOffset that represents the end of a user's lockout, any time in the past should be considered
|
||||
/// not locked out.
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<DateTimeOffset> GetLockoutEndDate(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return
|
||||
Task.FromResult(user.LockoutEndDateUtc.HasValue
|
||||
? new DateTimeOffset(DateTime.SpecifyKind(user.LockoutEndDateUtc.Value, DateTimeKind.Utc))
|
||||
: new DateTimeOffset());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locks a user out until the specified end date (set to a past date, to unlock a user)
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="lockoutEnd"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetLockoutEndDate(TUser user, DateTimeOffset lockoutEnd, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.LockoutEndDateUtc = lockoutEnd == DateTimeOffset.MinValue ? (DateTime?)null : lockoutEnd.UtcDateTime;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to record when an attempt to access the user has failed
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<int> IncrementAccessFailedCount(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.AccessFailedCount++;
|
||||
return Task.FromResult(user.AccessFailedCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to reset the account access count, typically after the account is successfully accessed
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task ResetAccessFailedCount(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.AccessFailedCount = 0;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current number of failed access attempts. This number usually will be reset whenever the password is
|
||||
/// verified or the account is locked out.
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<int> GetAccessFailedCount(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.AccessFailedCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the user can be locked out.
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> GetLockoutEnabled(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.LockoutEnabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets whether the user can be locked out.
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="enabled"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetLockoutEnabled(TUser user, bool enabled, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.LockoutEnabled = enabled;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the user's phone number
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="phoneNumber"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetPhoneNumber(TUser user, string phoneNumber, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.PhoneNumber = phoneNumber;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a user's phone number
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<string> GetPhoneNumber(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.PhoneNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the user phoneNumber is confirmed
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> GetPhoneNumberConfirmed(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.PhoneNumberConfirmed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set PhoneNumberConfirmed on the user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="confirmed"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetPhoneNumberConfirmed(TUser user, bool confirmed, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.PhoneNumberConfirmed = confirmed;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a user to a role
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="roleName"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task AddToRole(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
// TODO:
|
||||
//if (String.IsNullOrWhiteSpace(roleName))
|
||||
//{
|
||||
// throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
|
||||
//}
|
||||
var roleEntity = Context.Set<TRole>().SingleOrDefault(r => r.Name.ToUpper() == roleName.ToUpper());
|
||||
if (roleEntity == null)
|
||||
{
|
||||
throw new InvalidOperationException("Role Not Found");
|
||||
//TODO: String.Format(CultureInfo.CurrentCulture, IdentityResources.RoleNotFound, roleName));
|
||||
}
|
||||
var ur = new TUserRole { UserId = user.Id, RoleId = roleEntity.Id };
|
||||
user.Roles.Add(ur);
|
||||
roleEntity.Users.Add(ur);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a user from a role
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="roleName"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task RemoveFromRole(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
//if (String.IsNullOrWhiteSpace(roleName))
|
||||
//{
|
||||
// throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
|
||||
//}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the names of the roles a user is a member of
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<IList<string>> GetRoles(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the user is in the named role
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="roleName"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> IsInRole(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
//if (String.IsNullOrWhiteSpace(roleName))
|
||||
//{
|
||||
// throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
|
||||
//}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the security stamp for the user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="stamp"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetSecurityStamp(TUser user, string stamp, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.SecurityStamp = stamp;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the security stamp for a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<string> GetSecurityStamp(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.SecurityStamp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set whether two factor authentication is enabled for the user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="enabled"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task SetTwoFactorEnabled(TUser user, bool enabled, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
user.TwoFactorEnabled = enabled;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether two factor authentication is enabled for the user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<bool> GetTwoFactorEnabled(TUser user, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
ThrowIfDisposed();
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentNullException("user");
|
||||
}
|
||||
return Task.FromResult(user.TwoFactorEnabled);
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose the store
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,40 @@
|
|||
{
|
||||
"version": "0.1-alpha-*",
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Identity": "0.1-alpha-*"
|
||||
"Microsoft.AspNet.ConfigurationModel": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Identity": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Logging": "0.1-alpha-*",
|
||||
"Microsoft.Data.Entity": "0.1-alpha-*",
|
||||
"Microsoft.Data.Relational": "0.1-alpha-*",
|
||||
"Microsoft.Data.SqlServer": "0.1-pre-*",
|
||||
"Microsoft.Data.InMemory": "0.1-alpha-*",
|
||||
"System.Security.Claims" : "0.1-alpha-*"
|
||||
},
|
||||
"configurations": {
|
||||
"net45": {},
|
||||
"net45": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "",
|
||||
"System.Collections": ""
|
||||
}
|
||||
},
|
||||
"k10": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.0.20.0"
|
||||
"System.Collections": "4.0.0.0",
|
||||
"System.ComponentModel": "4.0.0.0",
|
||||
"System.Diagnostics.Debug": "4.0.10.0",
|
||||
"System.Diagnostics.Tools": "4.0.0.0",
|
||||
"System.Globalization": "4.0.10.0",
|
||||
"System.Linq": "4.0.0.0",
|
||||
"System.Linq.Expressions": "4.0.0.0",
|
||||
"System.Linq.Queryable": "4.0.0.0",
|
||||
"System.Reflection": "4.0.10.0",
|
||||
"System.Resources.ResourceManager": "4.0.0.0",
|
||||
"System.Runtime": "4.0.20.0",
|
||||
"System.Runtime.Extensions": "4.0.10.0",
|
||||
"System.Security.Principal": "4.0.0.0",
|
||||
"System.Text.Encoding": "4.0.20.0",
|
||||
"System.Threading.Tasks": "4.0.10.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,17 +6,18 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace Microsoft.AspNet.Identity.InMemory
|
||||
{
|
||||
public class InMemoryRoleStore : IQueryableRoleStore<InMemoryRole, string>
|
||||
{
|
||||
private readonly Dictionary<string, InMemoryRole> _roles = new Dictionary<string, InMemoryRole>();
|
||||
public class InMemoryRoleStore<TRole> : IQueryableRoleStore<TRole, string> where TRole : class,IRole<string>
|
||||
|
||||
public Task Create(InMemoryRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
private readonly Dictionary<string, TRole> _roles = new Dictionary<string, TRole>();
|
||||
|
||||
public Task Create(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_roles[role.Id] = role;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task Delete(InMemoryRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public Task Delete(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (role == null || !_roles.ContainsKey(role.Id))
|
||||
{
|
||||
|
|
@ -26,22 +27,22 @@ namespace Microsoft.AspNet.Identity.InMemory
|
|||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task Update(InMemoryRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public Task Update(TRole role, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_roles[role.Id] = role;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task<InMemoryRole> FindById(string roleId, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public Task<TRole> FindById(string roleId, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (_roles.ContainsKey(roleId))
|
||||
{
|
||||
return Task.FromResult(_roles[roleId]);
|
||||
}
|
||||
return Task.FromResult<InMemoryRole>(null);
|
||||
return Task.FromResult<TRole>(null);
|
||||
}
|
||||
|
||||
public Task<InMemoryRole> FindByName(string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public Task<TRole> FindByName(string roleName, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return
|
||||
Task.FromResult(
|
||||
|
|
@ -52,7 +53,7 @@ namespace Microsoft.AspNet.Identity.InMemory
|
|||
{
|
||||
}
|
||||
|
||||
public IQueryable<InMemoryRole> Roles
|
||||
public IQueryable<TRole> Roles
|
||||
{
|
||||
get { return _roles.Values.AsQueryable(); }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
"System.Globalization": "4.0.10.0",
|
||||
"System.Linq": "4.0.0.0",
|
||||
"System.Linq.Expressions": "4.0.0.0",
|
||||
"System.Linq.Queryable": "4.0.0.0",
|
||||
"System.Reflection": "4.0.10.0",
|
||||
"System.Resources.ResourceManager": "4.0.0.0",
|
||||
"System.Runtime": "4.0.20.0",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Identity.Entity.Test
|
||||
{
|
||||
public static class IdentityResultAssert
|
||||
{
|
||||
public static void IsSuccess(IdentityResult result)
|
||||
{
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result.Succeeded);
|
||||
}
|
||||
|
||||
public static void IsFailure(IdentityResult result)
|
||||
{
|
||||
Assert.NotNull(result);
|
||||
Assert.False(result.Succeeded);
|
||||
}
|
||||
|
||||
public static void IsFailure(IdentityResult result, string error)
|
||||
{
|
||||
Assert.NotNull(result);
|
||||
Assert.False(result.Succeeded);
|
||||
Assert.Equal(error, result.Errors.First());
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,54 @@
|
|||
{
|
||||
"version": "0.1-alpha-*",
|
||||
"dependencies": {
|
||||
"Microsoft.Bcl.Immutable": "1.1.18-beta-*",
|
||||
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.ConfigurationModel": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Identity": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Identity.Entity": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Logging": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Security.DataProtection": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Testing": "0.1-alpha-*",
|
||||
"Microsoft.Data.Entity": "0.1-alpha-*",
|
||||
"Microsoft.Data.InMemory": "0.1-alpha-*",
|
||||
"Microsoft.Data.Relational": "0.1-alpha-*",
|
||||
"Microsoft.Data.SqlServer": "0.1-pre-*",
|
||||
"System.Security.Claims": "0.1-alpha-*",
|
||||
"Xunit.KRunner": "0.1-alpha-*",
|
||||
"xunit.abstractions": "2.0.0-aspnet-*",
|
||||
"xunit.assert": "2.0.0-aspnet-*",
|
||||
"xunit.core": "2.0.0-aspnet-*",
|
||||
"xunit.execution": "2.0.0-aspnet-*"
|
||||
},
|
||||
"configurations": {
|
||||
"net45": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "",
|
||||
"System.Collections": "",
|
||||
"System.Data": ""
|
||||
}
|
||||
},
|
||||
"k10": {
|
||||
"dependencies": {
|
||||
"System.Collections": "4.0.0.0",
|
||||
"System.ComponentModel": "4.0.0.0",
|
||||
"System.Diagnostics.Debug": "4.0.10.0",
|
||||
"System.Diagnostics.Tools": "4.0.0.0",
|
||||
"System.Globalization": "4.0.10.0",
|
||||
"System.Linq": "4.0.0.0",
|
||||
"System.Linq.Expressions": "4.0.0.0",
|
||||
"System.Linq.Queryable": "4.0.0.0",
|
||||
"System.Reflection": "4.0.10.0",
|
||||
"System.Resources.ResourceManager": "4.0.0.0",
|
||||
"System.Runtime": "4.0.20.0",
|
||||
"System.Runtime.Extensions": "4.0.10.0",
|
||||
"System.Security.Principal": "4.0.0.0",
|
||||
"System.Text.Encoding": "4.0.20.0",
|
||||
"System.Threading.Tasks": "4.0.10.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"commands": {
|
||||
}
|
||||
}
|
||||
|
|
@ -1413,7 +1413,7 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
|
|||
|
||||
private static RoleManager<InMemoryRole> CreateRoleManager()
|
||||
{
|
||||
return new RoleManager<InMemoryRole>(new InMemoryRoleStore());
|
||||
return new RoleManager<InMemoryRole>(new InMemoryRoleStore<InMemoryRole>());
|
||||
}
|
||||
|
||||
public class TestMessageService : IIdentityMessageService
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.AspNet.DependencyInjection;
|
||||
using Microsoft.AspNet.DependencyInjection.Fallback;
|
||||
|
||||
|
|
@ -7,51 +8,17 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
{
|
||||
public static class TestServices
|
||||
{
|
||||
public static IServiceProvider DefaultServiceProvider<TUser, TKey>()
|
||||
where TUser : class, IUser<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var serviceCollection = new ServiceCollection {DefaultServices<TUser, TKey>()};
|
||||
return serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public static IEnumerable<IServiceDescriptor> DefaultServices<TUser, TKey>()
|
||||
where TUser : class, IUser<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return new IServiceDescriptor[]
|
||||
{
|
||||
new ServiceDescriptor<IPasswordValidator, PasswordValidator>(),
|
||||
new ServiceDescriptor<IUserValidator<TUser, TKey>, UserValidator<TUser, TKey>>(),
|
||||
new ServiceDescriptor<IPasswordHasher, PasswordHasher>(),
|
||||
new ServiceDescriptor<IClaimsIdentityFactory<TUser, TKey>, ClaimsIdentityFactory<TUser, TKey>>(),
|
||||
new ServiceDescriptor<IUserStore<TUser, TKey>, NoopUserStore>()
|
||||
};
|
||||
var describer = new ServiceDescriber();
|
||||
yield return describer.Transient<IPasswordValidator, PasswordValidator>();
|
||||
yield return describer.Transient<IUserValidator<TUser, TKey>, UserValidator<TUser, TKey>>();
|
||||
yield return describer.Transient<IPasswordHasher, PasswordHasher>();
|
||||
yield return describer.Transient<IClaimsIdentityFactory<TUser, TKey>, ClaimsIdentityFactory<TUser, TKey>>();
|
||||
yield return describer.Transient<IUserStore<TUser, TKey>, NoopUserStore>();
|
||||
}
|
||||
|
||||
public class ServiceDescriptor<TService, TImplementation> : IServiceDescriptor
|
||||
{
|
||||
public ServiceDescriptor(LifecycleKind lifecycle = LifecycleKind.Transient)
|
||||
{
|
||||
Lifecycle = lifecycle;
|
||||
}
|
||||
|
||||
public LifecycleKind Lifecycle { get; private set; }
|
||||
|
||||
public Type ServiceType
|
||||
{
|
||||
get { return typeof (TService); }
|
||||
}
|
||||
|
||||
public Type ImplementationType
|
||||
{
|
||||
get { return typeof (TImplementation); }
|
||||
}
|
||||
|
||||
public object ImplementationInstance
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.DependencyInjection.Fallback;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
|
@ -22,7 +23,7 @@ namespace Microsoft.AspNet.Identity.Test
|
|||
[Fact]
|
||||
public void ServiceProviderWireupTest()
|
||||
{
|
||||
var manager = new TestManager(TestServices.DefaultServiceProvider<TestUser, string>());
|
||||
var manager = new TestManager(TestServices.DefaultServices<TestUser, string>().BuildServiceProvider());
|
||||
Assert.NotNull(manager.PasswordHasher);
|
||||
Assert.NotNull(manager.PasswordValidator);
|
||||
Assert.NotNull(manager.UserValidator);
|
||||
|
|
|
|||
Loading…
Reference in New Issue