Move POCO to Core

- Move POCO types to core
- Keep Entity pocos initially (will try to remove soon)
- Reenable Entity unit tests now that blocking data bugs have been fixed
This commit is contained in:
Hao Kung 2014-04-11 15:00:49 -07:00
parent 1998b65c52
commit 538e8dabea
38 changed files with 2415 additions and 2060 deletions

View File

@ -6,12 +6,12 @@ namespace Microsoft.AspNet.Identity.Entity
/// <summary>
/// Represents a Role entity
/// </summary>
public class IdentityRole : IdentityRole<string, IdentityUserRole>
public class EntityRole : EntityRole<string, IdentityUserRole>
{
/// <summary>
/// Constructor
/// </summary>
public IdentityRole()
public EntityRole()
{
Id = Guid.NewGuid().ToString();
}
@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Identity.Entity
/// Constructor
/// </summary>
/// <param name="roleName"></param>
public IdentityRole(string roleName)
public EntityRole(string roleName)
: this()
{
Name = roleName;
@ -32,14 +32,14 @@ namespace Microsoft.AspNet.Identity.Entity
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TUserRole"></typeparam>
public class IdentityRole<TKey, TUserRole>
public class EntityRole<TKey, TUserRole>
where TUserRole : IdentityUserRole<TKey>
where TKey : IEquatable<TKey>
{
/// <summary>
/// Constructor
/// </summary>
public IdentityRole()
public EntityRole()
{
Users = new List<TUserRole>();
}
@ -60,3 +60,4 @@ namespace Microsoft.AspNet.Identity.Entity
public virtual string Name { get; set; }
}
}

View File

@ -1,35 +1,34 @@

using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.AspNet.Identity.Entity
{
public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
public class EntityUser : EntityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public IdentityUser()
public EntityUser()
{
Id = Guid.NewGuid().ToString();
}
public IdentityUser(string userName) : this()
public EntityUser(string userName)
: this()
{
UserName = userName;
}
}
public class IdentityUser<TKey, TLogin, TRole, TClaim>
public class EntityUser<TKey, TLogin, TRole, TClaim>
where TLogin : IdentityUserLogin<TKey>
where TRole : IdentityUserRole<TKey>
where TClaim : IdentityUserClaim<TKey>
where TKey : IEquatable<TKey>
{
public IdentityUser()
public EntityUser()
{
Claims = new List<TClaim>();
Roles = new List<TRole>();
Logins = new List<TLogin>();
}
public virtual TKey Id { get; set; }
@ -73,7 +72,7 @@ namespace Microsoft.AspNet.Identity.Entity
/// <summary>
/// DateTime in UTC when lockout ends, any time in the past is considered not locked out.
/// </summary>
public virtual DateTime? LockoutEndDateUtc { get; set; }
public virtual DateTime? LockoutEnd { get; set; }
/// <summary>
/// Is lockout enabled for this user
@ -102,3 +101,4 @@ namespace Microsoft.AspNet.Identity.Entity
}
}

View File

@ -1,21 +1,20 @@
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>
IdentityContext<EntityUser, EntityRole, 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 TUser : EntityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : EntityRole<TKey, TUserRole> /*, TUserRole*/
where TUserLogin : IdentityUserLogin<TKey>
where TUserRole : IdentityUserRole<TKey>
where TUserClaim : IdentityUserClaim<TKey>
@ -48,13 +47,15 @@ namespace Microsoft.AspNet.Identity.Entity
//.ToTable("AspNetRoles");
builder.Entity<TUserRole>()
.Key(r => new {r.UserId, r.RoleId})
.Key(u => u.Id)
//TODO: .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})
.Key(u => u.Id)
//TODO: .Key(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
.ForeignKeys(fk => fk.ForeignKey<TUser>(f => f.UserId));
//.ToTable("AspNetUserLogins");

View File

@ -20,12 +20,18 @@
<Content Include="Project.json" />
</ItemGroup>
<ItemGroup>
<<<<<<< HEAD
<Compile Include="IdentityContext.cs" />
<Compile Include="IdentityRole.cs" />
<Compile Include="IdentityUser.cs" />
<Compile Include="IdentityUserClaim.cs" />
<Compile Include="IdentityUserLogin.cs" />
<Compile Include="IdentityUserRole.cs" />
=======
<Compile Include="EntityRole.cs" />
<Compile Include="EntityUser.cs" />
<Compile Include="IdentityContext.cs" />
>>>>>>> POCO
<Compile Include="RoleStore.cs" />
<Compile Include="UserStore.cs" />
</ItemGroup>

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNet.Identity.Entity
{
public class RoleStore<TRole, TKey> :
IQueryableRoleStore<TRole>
where TRole : IdentityRole
where TRole : EntityRole
where TKey : IEquatable<TKey>
{
private bool _disposed;
@ -41,7 +41,8 @@ namespace Microsoft.AspNet.Identity.Entity
public virtual Task<TRole> GetRoleAggregate(Expression<Func<TRole, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return Roles.SingleOrDefaultAsync(filter, cancellationToken);
// TODO: return Roles.SingleOrDefaultAsync(filter, cancellationToken);
return Task.FromResult(Roles.SingleOrDefault(filter));
}
public async virtual Task CreateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
@ -82,14 +83,38 @@ namespace Microsoft.AspNet.Identity.Entity
public Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (role == null)
{
throw new ArgumentNullException("role");
}
return Task.FromResult(role.Id);
}
public Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (role == null)
{
throw new ArgumentNullException("role");
}
return Task.FromResult(role.Name);
}
public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (role == null)
{
throw new ArgumentNullException("role");
}
role.Name = roleName;
return Task.FromResult(0);
}
public virtual TKey ConvertId(string userId)
{
@ -97,7 +122,7 @@ namespace Microsoft.AspNet.Identity.Entity
}
/// <summary>
/// FindByLoginAsync a role by id
/// Find a role by id
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
@ -107,12 +132,11 @@ namespace Microsoft.AspNet.Identity.Entity
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
var roleId = ConvertId(id);
return Roles.SingleOrDefaultAsync(r => r.Id.Equals(roleId), cancellationToken);
//return GetRoleAggregate(u => u.Id.Equals(id));
return GetRoleAggregate(u => u.Id.Equals(roleId), cancellationToken);
}
/// <summary>
/// FindByLoginAsync a role by name
/// Find a role by name
/// </summary>
/// <param name="name"></param>
/// <param name="cancellationToken"></param>
@ -121,8 +145,7 @@ namespace Microsoft.AspNet.Identity.Entity
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == name.ToUpper(), cancellationToken);
//return GetRoleAggregate(u => u.Name.ToUpper() == name.ToUpper());
return GetRoleAggregate(u => u.Name.ToUpper() == name.ToUpper(), cancellationToken);
}
private void ThrowIfDisposed()

View File

@ -11,7 +11,12 @@ using Microsoft.Data.Entity;
namespace Microsoft.AspNet.Identity.Entity
{
public class UserStore :
UserStore<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
UserStore<EntityUser>
{
public UserStore(EntityContext context) : base(context) { }
}
public class UserStore<TUser> : UserStore<TUser, EntityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> where TUser:EntityUser
{
public UserStore(EntityContext context) : base(context) { }
}
@ -28,8 +33,8 @@ namespace Microsoft.AspNet.Identity.Entity
IUserTwoFactorStore<TUser>,
IUserLockoutStore<TUser>
where TKey : IEquatable<TKey>
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : IdentityRole<TKey, TUserRole>
where TUser : EntityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : EntityRole<TKey, TUserRole>
where TUserLogin : IdentityUserLogin<TKey>, new()
where TUserRole : IdentityUserRole<TKey>, new()
where TUserClaim : IdentityUserClaim<TKey>, new()
@ -60,7 +65,8 @@ namespace Microsoft.AspNet.Identity.Entity
protected virtual Task<TUser> GetUserAggregate(Expression<Func<TUser, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return Users.SingleOrDefaultAsync(filter, cancellationToken);
return Task.FromResult(Users.SingleOrDefault(filter));
// TODO: return Users.SingleOrDefaultAsync(filter, cancellationToken);
//Include(u => u.Roles)
//.Include(u => u.Claims)
//.Include(u => u.Logins)
@ -142,7 +148,7 @@ namespace Microsoft.AspNet.Identity.Entity
}
/// <summary>
/// FindByLoginAsync a user by id
/// Find a user by id
/// </summary>
/// <param name="userId"></param>
/// <param name="cancellationToken"></param>
@ -152,12 +158,11 @@ namespace Microsoft.AspNet.Identity.Entity
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
var id = ConvertUserId(userId);
return Users.SingleOrDefaultAsync(u => u.Id.Equals(id), cancellationToken);
// TODO: return GetUserAggregate(u => u.Id.Equals(userId), cancellationToken);
return GetUserAggregate(u => u.Id.Equals(id), cancellationToken);
}
/// <summary>
/// FindByLoginAsync a user by name
/// Find a user by name
/// </summary>
/// <param name="userName"></param>
/// <param name="cancellationToken"></param>
@ -166,8 +171,7 @@ namespace Microsoft.AspNet.Identity.Entity
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Users.SingleOrDefaultAsync(u => u.UserName.ToUpper() == userName.ToUpper(), cancellationToken);
// TODO: return GetUserAggregate(u => u.UserName.ToUpper() == userName.ToUpper(), cancellationToken);
return GetUserAggregate(u => u.UserName.ToUpper() == userName.ToUpper(), cancellationToken);
}
public IQueryable<TUser> Users
@ -205,7 +209,19 @@ namespace Microsoft.AspNet.Identity.Entity
{
throw new ArgumentNullException("user");
}
throw new NotImplementedException();
if (login == null)
{
throw new ArgumentNullException("login");
}
var provider = login.LoginProvider;
var key = login.ProviderKey;
var entry = user.Logins.SingleOrDefault(l => l.LoginProvider == provider && l.ProviderKey == key);
if (entry != null)
{
user.Logins.Remove(entry);
Context.Set<TUserLogin>().Remove(entry);
}
return Task.FromResult(0);
}
public virtual Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
@ -468,8 +484,8 @@ namespace Microsoft.AspNet.Identity.Entity
throw new ArgumentNullException("user");
}
return
Task.FromResult(user.LockoutEndDateUtc.HasValue
? new DateTimeOffset(DateTime.SpecifyKind(user.LockoutEndDateUtc.Value, DateTimeKind.Utc))
Task.FromResult(user.LockoutEnd.HasValue
? new DateTimeOffset(DateTime.SpecifyKind(user.LockoutEnd.Value, DateTimeKind.Utc))
: new DateTimeOffset());
}
@ -488,7 +504,7 @@ namespace Microsoft.AspNet.Identity.Entity
{
throw new ArgumentNullException("user");
}
user.LockoutEndDateUtc = lockoutEnd == DateTimeOffset.MinValue ? (DateTime?)null : lockoutEnd.UtcDateTime;
user.LockoutEnd = lockoutEnd == DateTimeOffset.MinValue ? (DateTime?)null : lockoutEnd.UtcDateTime;
return Task.FromResult(0);
}
@ -705,7 +721,17 @@ namespace Microsoft.AspNet.Identity.Entity
//{
// throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
//}
throw new NotImplementedException();
var roleEntity = Context.Set<TRole>().SingleOrDefault(r => r.Name.ToUpper() == roleName.ToUpper());
if (roleEntity != null)
{
var userRole = user.Roles.FirstOrDefault(r => roleEntity.Id.Equals(r.RoleId));
if (userRole != null)
{
user.Roles.Remove(userRole);
roleEntity.Users.Remove(userRole);
}
}
return Task.FromResult(0);
}
/// <summary>
@ -722,7 +748,11 @@ namespace Microsoft.AspNet.Identity.Entity
{
throw new ArgumentNullException("user");
}
throw new NotImplementedException();
var query = from userRoles in user.Roles
join roles in Context.Set<TRole>()
on userRoles.RoleId equals roles.Id
select roles.Name;
return Task.FromResult<IList<string>>(query.ToList());
}
/// <summary>
@ -744,7 +774,11 @@ namespace Microsoft.AspNet.Identity.Entity
//{
// throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
//}
throw new NotImplementedException();
var any =
Context.Set<TRole>().Where(r => r.Name.ToUpper() == roleName.ToUpper())
.Where(r => r.Users.Any(ur => ur.UserId.Equals(user.Id)))
.Count() > 0;
return Task.FromResult(any);
}
/// <summary>

View File

@ -7,7 +7,6 @@
"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-*"
},

View File

@ -1,16 +0,0 @@
using System;
namespace Microsoft.AspNet.Identity.InMemory
{
public class InMemoryRole
{
public InMemoryRole(string roleName)
{
Id = Guid.NewGuid().ToString();
Name = roleName;
}
public virtual string Id { get; set; }
public virtual string Name { get; set; }
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Microsoft.AspNet.Identity.InMemory
{
public class InMemoryRoleStore<TRole> : IQueryableRoleStore<TRole> where TRole : InMemoryRole
public class InMemoryRoleStore<TRole> : IQueryableRoleStore<TRole> where TRole : IdentityRole
{
private readonly Dictionary<string, TRole> _roles = new Dictionary<string, TRole>();
@ -37,6 +37,12 @@ namespace Microsoft.AspNet.Identity.InMemory
return Task.FromResult(role.Name);
}
public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = new CancellationToken())
{
role.Name = roleName;
return Task.FromResult(0);
}
public Task UpdateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
{
_roles[role.Id] = role;

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNet.Identity.InMemory
IUserPhoneNumberStore<TUser>,
IQueryableUserStore<TUser>,
IUserTwoFactorStore<TUser>
where TUser : InMemoryUser
where TUser : IdentityUser
{
private readonly Dictionary<UserLoginInfo, TUser> _logins =
new Dictionary<UserLoginInfo, TUser>(new LoginComparer());
@ -32,18 +32,25 @@ namespace Microsoft.AspNet.Identity.InMemory
public Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(user.Claims);
var claims = user.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
return Task.FromResult<IList<Claim>>(claims);
}
public Task AddClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
{
user.Claims.Add(claim);
user.Claims.Add(new IdentityUserClaim<string> { ClaimType = claim.Type, ClaimValue = claim.Value, UserId = user.Id });
return Task.FromResult(0);
}
public Task RemoveClaimAsync(TUser user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
{
user.Claims.Remove(claim);
var entity =
user.Claims.FirstOrDefault(
uc => uc.UserId == user.Id && uc.ClaimType == claim.Type && uc.ClaimValue == claim.Value);
if (entity != null)
{
user.Claims.Remove(entity);
}
return Task.FromResult(0);
}
@ -117,27 +124,35 @@ namespace Microsoft.AspNet.Identity.InMemory
public Task AddLoginAsync(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
{
user.Logins.Add(login);
user.Logins.Add(new IdentityUserLogin<string>
{
UserId = user.Id,
LoginProvider = login.LoginProvider,
ProviderKey = login.ProviderKey
});
_logins[login] = user;
return Task.FromResult(0);
}
public Task RemoveLoginAsync(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
{
var logs =
user.Logins.Where(l => l.ProviderKey == login.ProviderKey && l.LoginProvider == login.LoginProvider)
.ToList();
foreach (var l in logs)
var loginEntity =
user.Logins.SingleOrDefault(
l =>
l.ProviderKey == login.ProviderKey && l.LoginProvider == login.LoginProvider &&
l.UserId == user.Id);
if (loginEntity != null)
{
user.Logins.Remove(l);
_logins[l] = null;
user.Logins.Remove(loginEntity);
}
_logins[login] = null;
return Task.FromResult(0);
}
public Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(user.Logins);
var logins = user.Logins.Select(l => new UserLoginInfo(l.LoginProvider, l.ProviderKey)).ToList();
return Task.FromResult<IList<UserLoginInfo>>(logins);
}
public Task<TUser> FindByLoginAsync(UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
@ -245,26 +260,32 @@ namespace Microsoft.AspNet.Identity.InMemory
return Task.FromResult(0);
}
// RoleId == roleName for InMemory
public Task AddToRoleAsync(TUser user, string role, CancellationToken cancellationToken = default(CancellationToken))
{
user.Roles.Add(role);
user.Roles.Add(new IdentityUserRole<string> { RoleId = role, UserId = user.Id });
return Task.FromResult(0);
}
// RoleId == roleName for InMemory
public Task RemoveFromRoleAsync(TUser user, string role, CancellationToken cancellationToken = default(CancellationToken))
{
user.Roles.Remove(role);
var roleEntity = user.Roles.SingleOrDefault(ur => ur.RoleId == role);
if (roleEntity != null)
{
user.Roles.Remove(roleEntity);
}
return Task.FromResult(0);
}
public Task<IList<string>> GetRolesAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(user.Roles);
return Task.FromResult<IList<string>>(user.Roles.Select(ur => ur.RoleId).ToList());
}
public Task<bool> IsInRoleAsync(TUser user, string role, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(user.Roles.Contains(role));
return Task.FromResult(user.Roles.Any(ur => ur.RoleId == role));
}
public Task SetSecurityStampAsync(TUser user, string stamp, CancellationToken cancellationToken = default(CancellationToken))

View File

@ -20,9 +20,7 @@
<Content Include="Project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="InMemoryRole.cs" />
<Compile Include="InMemoryRoleStore.cs" />
<Compile Include="InMemoryUser.cs" />
<Compile Include="InMemoryUserStore.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\ProjectK\Microsoft.Web.ProjectK.targets" Condition="'$(VSToolsPath)' != ''" />

View File

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Abstractions.Security;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Abstractions.Security;
namespace Microsoft.AspNet.Identity.Security
{
@ -26,10 +24,10 @@ namespace Microsoft.AspNet.Identity.Security
{
return null;
}
return await UserManager.CreateIdentity(user, AuthenticationType);
return await UserManager.CreateIdentityAsync(user, AuthenticationType);
}
public virtual async Task SignIn(TUser user, bool isPersistent, bool rememberBrowser)
public virtual async Task SignInAsync(TUser user, bool isPersistent, bool rememberBrowser)
{
if (Context == null)
{
@ -143,7 +141,7 @@ namespace Microsoft.AspNet.Identity.Security
}
if (await UserManager.CheckPasswordAsync(user, password))
{
await SignIn(user, isPersistent, false);
await SignInAsync(user, isPersistent, false);
return SignInStatus.Success;
//TODO: return await SignInOrTwoFactor(user, isPersistent);
}

View File

@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;

View File

@ -6,14 +6,14 @@ using System.Threading.Tasks;
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Interface for creating a ClaimsIdentity from an IUser
/// Interface for creating a ClaimsIdentity from an user
/// </summary>
/// <typeparam name="TUser"></typeparam>
public interface IClaimsIdentityFactory<TUser>
where TUser : class
{
/// <summary>
/// CreateAsync a ClaimsIdentity from an user using a UserManager
/// Create a ClaimsIdentity from an user using a UserManager
/// </summary>
/// <param name="manager"></param>
/// <param name="user"></param>

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNet.Identity
public interface IPasswordValidator
{
/// <summary>
/// ValidateAsync the item
/// Validate the item
/// </summary>
/// <returns></returns>
Task<IdentityResult> ValidateAsync(string password, CancellationToken cancellationToken = default(CancellationToken));

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNet.Identity
public interface IQueryableRoleStore<TRole> : IRoleStore<TRole> where TRole : class
{
/// <summary>
/// IQueryable users
/// IQueryable roles
/// </summary>
IQueryable<TRole> Roles { get; }
}

View File

@ -6,7 +6,6 @@ namespace Microsoft.AspNet.Identity
/// Interface that exposes an IQueryable users
/// </summary>
/// <typeparam name="TUser"></typeparam>
/// <typeparam name="TKey"></typeparam>
public interface IQueryableUserStore<TUser> : IUserStore<TUser> where TUser : class
{
/// <summary>

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Identity
Task CreateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// UpdateAsync a role
/// Update a role
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
@ -50,6 +50,15 @@ namespace Microsoft.AspNet.Identity
/// <returns></returns>
Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Set a role's name
/// </summary>
/// <param name="role"></param>
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Finds a role by id
/// </summary>
@ -59,7 +68,7 @@ namespace Microsoft.AspNet.Identity
Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// FindByLoginAsync a role by name
/// Find a role by name
/// </summary>
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Represents a Role entity
/// </summary>
public class IdentityRole : IdentityRole<string>
{
/// <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>
public class IdentityRole<TKey> where TKey : IEquatable<TKey>
{
/// <summary>
/// Constructor
/// </summary>
public IdentityRole()
{
Users = new List<IdentityUserRole<TKey>>();
}
/// <summary>
/// Navigation property for users in the role
/// </summary>
public virtual ICollection<IdentityUserRole<TKey>> Users { get; private set; }
/// <summary>
/// Role id
/// </summary>
public virtual TKey Id { get; set; }
/// <summary>
/// Role name
/// </summary>
public virtual string Name { get; set; }
}
}

View File

@ -1,27 +1,33 @@
using System;
using System;
using System.Collections.Generic;
using System.Security.Claims;
namespace Microsoft.AspNet.Identity.InMemory
namespace Microsoft.AspNet.Identity
{
public class InMemoryUser
public class IdentityUser : IdentityUser<string>
{
private readonly IList<Claim> _claims;
private readonly IList<UserLoginInfo> _logins;
private readonly IList<string> _roles;
public InMemoryUser()
public IdentityUser()
{
Id = Guid.NewGuid().ToString();
_logins = new List<UserLoginInfo>();
_claims = new List<Claim>();
_roles = new List<string>();
}
public InMemoryUser(string name) : this()
public IdentityUser(string userName) : this()
{
UserName = name;
UserName = userName;
}
}
public class IdentityUser<TKey>
where TKey : IEquatable<TKey>
{
public IdentityUser()
{
Claims = new List<IdentityUserClaim<TKey>>();
Roles = new List<IdentityUserRole<TKey>>();
Logins = new List<IdentityUserLogin<TKey>>();
}
public virtual TKey Id { get; set; }
public virtual string UserName { get; set; }
/// <summary>
/// Email
@ -73,22 +79,20 @@ namespace Microsoft.AspNet.Identity.InMemory
/// </summary>
public virtual int AccessFailedCount { get; set; }
public IList<UserLoginInfo> Logins
{
get { return _logins; }
}
/// <summary>
/// Roles for the user
/// </summary>
public virtual ICollection<IdentityUserRole<TKey>> Roles { get; private set; }
public IList<Claim> Claims
{
get { return _claims; }
}
/// <summary>
/// Claims for the user
/// </summary>
public virtual ICollection<IdentityUserClaim<TKey>> Claims { get; private set; }
public IList<string> Roles
{
get { return _roles; }
}
/// <summary>
/// Associated logins for the user
/// </summary>
public virtual ICollection<IdentityUserLogin<TKey>> Logins { get; private set; }
public virtual string Id { get; set; }
public virtual string UserName { get; set; }
}
}
}

View File

@ -1,6 +1,6 @@
using System;
namespace Microsoft.AspNet.Identity.Entity
namespace Microsoft.AspNet.Identity
{
public class IdentityUserClaim : IdentityUserClaim<string> { }

View File

@ -1,6 +1,6 @@
using System;
namespace Microsoft.AspNet.Identity.Entity
namespace Microsoft.AspNet.Identity
{
public class IdentityUserLogin : IdentityUserLogin<string> { }
@ -10,6 +10,13 @@ namespace Microsoft.AspNet.Identity.Entity
/// <typeparam name="TKey"></typeparam>
public class IdentityUserLogin<TKey> where TKey : IEquatable<TKey>
{
// TODO: Remove
public virtual string Id
{
get;
set;
}
/// <summary>
/// The login provider for the login (i.e. facebook, google)
/// </summary>

View File

@ -1,6 +1,6 @@
using System;
namespace Microsoft.AspNet.Identity.Entity
namespace Microsoft.AspNet.Identity
{
public class IdentityUserRole : IdentityUserRole<string> { }
@ -10,6 +10,13 @@ namespace Microsoft.AspNet.Identity.Entity
/// <typeparam name="TKey"></typeparam>
public class IdentityUserRole<TKey> where TKey : IEquatable<TKey>
{
// TODO: Remove
public virtual string Id
{
get;
set;
}
/// <summary>
/// UserId for the user that is in the role
/// </summary>

View File

@ -157,7 +157,7 @@ namespace Microsoft.AspNet.Identity
throw new ArgumentNullException("roleName");
}
return await FindByName(roleName, cancellationToken) != null;
return await FindByNameAsync(roleName, cancellationToken) != null;
}
/// <summary>
@ -184,13 +184,27 @@ namespace Microsoft.AspNet.Identity
return await Store.GetRoleNameAsync(role, cancellationToken);
}
/// <summary>
/// Set the name of the role
/// </summary>
/// <param name="role"></param>
/// <param name="name"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> SetRoleNameAsync(TRole role, string name, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
await Store.SetRoleNameAsync(role, name, cancellationToken);
return IdentityResult.Success;
}
/// <summary>
/// Return the role id for a role
/// </summary>
/// <param name="role"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> GetRoleId(TRole role, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
return await Store.GetRoleIdAsync(role, cancellationToken);
@ -202,7 +216,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<TRole> FindByName(string roleName, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<TRole> FindByNameAsync(string roleName, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (roleName == null)

View File

@ -48,8 +48,8 @@ namespace Microsoft.AspNet.Identity
}
else
{
var owner = await manager.FindByName(roleName);
if (owner != null && !string.Equals(await manager.GetRoleId(owner), await manager.GetRoleId(role)))
var owner = await manager.FindByNameAsync(roleName);
if (owner != null && !string.Equals(await manager.GetRoleIdAsync(owner), await manager.GetRoleIdAsync(role)))
{
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateName, roleName));
}

View File

@ -311,7 +311,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="authenticationType"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task<ClaimsIdentity> CreateIdentity(TUser user, string authenticationType, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -329,7 +329,7 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// CreateAsync a user with no password
/// Create a user with no password
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
@ -352,7 +352,7 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// UpdateAsync a user
/// Update a user
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
@ -374,7 +374,7 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// DeleteAsync a user
/// Delete a user
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
@ -391,7 +391,7 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// FindByLoginAsync a user by id
/// Find a user by id
/// </summary>
/// <param name="userId"></param>
/// <param name="cancellationToken"></param>
@ -403,7 +403,7 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// FindByLoginAsync a user by name
/// Find a user by name
/// </summary>
/// <param name="userName"></param>
/// <param name="cancellationToken"></param>
@ -430,10 +430,11 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// CreateAsync a user and associates it with the given password (if one is provided)
/// Create a user and associates it with the given password
/// </summary>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> CreateAsync(TUser user, string password, CancellationToken cancellationToken = default(CancellationToken))
{
@ -478,7 +479,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="userName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> SetUserName(TUser user, string userName, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IdentityResult> SetUserNameAsync(TUser user, string userName, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -508,7 +509,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="password"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<TUser> FindByUserNamePassword(string userName, string password, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<TUser> FindByUserNamePasswordAsync(string userName, string password, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var user = await FindByNameAsync(userName, cancellationToken);
@ -699,7 +700,7 @@ namespace Microsoft.AspNet.Identity
public virtual async Task<IdentityResult> UpdateSecurityStampAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var securityStore = GetSecurityStore();
GetSecurityStore();
if (user == null)
{
throw new ArgumentNullException("user");
@ -791,7 +792,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="login"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> RemoveLogin(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IdentityResult> RemoveLoginAsync(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var loginStore = GetLoginStore();
@ -811,11 +812,11 @@ namespace Microsoft.AspNet.Identity
/// <summary>
/// Associate a login with a user
/// </summary>
/// <param name="userId"></param>
/// <param name="user"></param>
/// <param name="login"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> AddLogin(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IdentityResult> AddLoginAsync(TUser user, UserLoginInfo login, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var loginStore = GetLoginStore();
@ -842,7 +843,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IList<UserLoginInfo>> GetLogins(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var loginStore = GetLoginStore();
@ -1030,6 +1031,7 @@ namespace Microsoft.AspNet.Identity
/// Get a user's email
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> GetEmailAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
@ -1047,6 +1049,7 @@ namespace Microsoft.AspNet.Identity
/// </summary>
/// <param name="user"></param>
/// <param name="email"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> SetEmailAsync(TUser user, string email, CancellationToken cancellationToken = default(CancellationToken))
{
@ -1066,6 +1069,7 @@ namespace Microsoft.AspNet.Identity
/// FindByLoginAsync a user by his email
/// </summary>
/// <param name="email"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task<TUser> FindByEmailAsync(string email, CancellationToken cancellationToken = default(CancellationToken))
{
@ -1237,7 +1241,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user"></param>
/// <param name="phoneNumber"></param>
/// <returns></returns>
public virtual async Task<string> GenerateChangePhoneNumberToken(TUser user, string phoneNumber)
public virtual async Task<string> GenerateChangePhoneNumberTokenAsync(TUser user, string phoneNumber)
{
ThrowIfDisposed();
return
@ -1336,7 +1340,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IList<string>> GetValidTwoFactorProviders(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IList<string>> GetValidTwoFactorProvidersAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -1443,7 +1447,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<bool> GetTwoFactorEnabled(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<bool> GetTwoFactorEnabledAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var store = GetUserTwoFactorStore();
@ -1477,14 +1481,14 @@ namespace Microsoft.AspNet.Identity
// SMS/Email methods
/// <summary>
/// SendAsync an email to the user
/// Send an email to the user
/// </summary>
/// <param name="user"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task SendEmail(TUser user, string subject, string body, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task SendEmailAsync(TUser user, string subject, string body, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -1504,13 +1508,13 @@ namespace Microsoft.AspNet.Identity
}
/// <summary>
/// SendAsync a user a sms message
/// Send a user a sms message
/// </summary>
/// <param name="user"></param>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task SendSms(TUser user, string message, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task SendSmsAsync(TUser user, string message, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -1566,6 +1570,7 @@ namespace Microsoft.AspNet.Identity
/// </summary>
/// <param name="user"></param>
/// <param name="enabled"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> SetLockoutEnabledAsync(TUser user, bool enabled, CancellationToken cancellationToken = default(CancellationToken))
{
@ -1583,6 +1588,7 @@ namespace Microsoft.AspNet.Identity
/// Returns whether the user allows lockout
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<bool> GetLockoutEnabledAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
@ -1601,7 +1607,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<DateTimeOffset> GetLockoutEndDate(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<DateTimeOffset> GetLockoutEndDateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var store = GetUserLockoutStore();
@ -1609,7 +1615,7 @@ namespace Microsoft.AspNet.Identity
{
throw new ArgumentNullException("user");
}
return await store.GetLockoutEndDateAsync(user);
return await store.GetLockoutEndDateAsync(user, cancellationToken);
}
/// <summary>
@ -1619,7 +1625,7 @@ namespace Microsoft.AspNet.Identity
/// <param name="lockoutEnd"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IdentityResult> SetLockoutEndDate(TUser user, DateTimeOffset lockoutEnd, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<IdentityResult> SetLockoutEndDateAsync(TUser user, DateTimeOffset lockoutEnd, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
var store = GetUserLockoutStore();
@ -1685,6 +1691,7 @@ namespace Microsoft.AspNet.Identity
/// Returns the number of failed access attempts for the user
/// </summary>
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<int> GetAccessFailedCountAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{

View File

@ -22,6 +22,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IdentityResultAssert.cs" />
<Compile Include="RoleStoreTest.cs" />
<Compile Include="TestIdentityFactory.cs" />
<Compile Include="UserStoreTest.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\ProjectK\Microsoft.Web.ProjectK.targets" Condition="'$(VSToolsPath)' != ''" />

View File

@ -0,0 +1,63 @@
using System;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNet.Identity.Entity.Test
{
public class RoleStoreTest
{
[Fact]
public async Task RoleStoreMethodsThrowWhenDisposedTest()
{
var store = new RoleStore<EntityRole, string>(new IdentityContext());
store.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.FindByNameAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetRoleIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.GetRoleNameAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.SetRoleNameAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.CreateAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.UpdateAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(async () => await store.DeleteAsync(null));
}
[Fact]
public async Task RoleStorePublicNullCheckTest()
{
Assert.Throws<ArgumentNullException>("context", () => new RoleStore<EntityRole, string>(null));
var store = new RoleStore<EntityRole, string>(new IdentityContext());
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.GetRoleIdAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.GetRoleNameAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.SetRoleNameAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.CreateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.UpdateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await store.DeleteAsync(null));
}
[Fact]
public async Task CanUpdateRoleName()
{
var manager = TestIdentityFactory.CreateRoleManager();
var role = new EntityRole("UpdateRoleName");
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
Assert.Null(await manager.FindByNameAsync("New"));
role.Name = "New";
IdentityResultAssert.IsSuccess(await manager.UpdateAsync(role));
Assert.NotNull(await manager.FindByNameAsync("New"));
Assert.Null(await manager.FindByNameAsync("UpdateAsync"));
}
[Fact]
public async Task CanSetUserName()
{
var manager = TestIdentityFactory.CreateRoleManager();
var role = new EntityRole("UpdateRoleName");
IdentityResultAssert.IsSuccess(await manager.CreateAsync(role));
Assert.Null(await manager.FindByNameAsync("New"));
IdentityResultAssert.IsSuccess(await manager.SetRoleNameAsync(role, "New"));
Assert.NotNull(await manager.FindByNameAsync("New"));
Assert.Null(await manager.FindByNameAsync("UpdateAsync"));
}
}
}

View File

@ -0,0 +1,101 @@
using Microsoft.AspNet.Testing;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.InMemory;
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNet.Identity.Entity.Test
{
public static class TestIdentityFactory
{
public static EntityContext CreateContext()
{
var configuration = new EntityConfigurationBuilder()
//.UseModel(model)
.UseDataStore(new InMemoryDataStore())
.BuildConfiguration();
var db = new IdentityContext(configuration);
// var sql = db.Configuration.DataStore as SqlServerDataStore;
// if (sql != null)
// {
//#if NET45
// var builder = new DbConnectionStringBuilder {ConnectionString = sql.ConnectionString};
// var targetDatabase = builder["Database"].ToString();
// // Connect to master, check if database exists, and create if not
// builder.Add("Database", "master");
// using (var masterConnection = new SqlConnection(builder.ConnectionString))
// {
// masterConnection.Open();
// var masterCommand = masterConnection.CreateCommand();
// masterCommand.CommandText = "SELECT COUNT(*) FROM sys.databases WHERE [name]=N'" + targetDatabase +
// "'";
// if ((int?) masterCommand.ExecuteScalar() < 1)
// {
// masterCommand.CommandText = "CREATE DATABASE [" + targetDatabase + "]";
// masterCommand.ExecuteNonQuery();
// using (var conn = new SqlConnection(sql.ConnectionString))
// {
// conn.Open();
// var command = conn.CreateCommand();
// command.CommandText = @"
//CREATE TABLE [dbo].[AspNetUsers] (
//[Id] NVARCHAR (128) NOT NULL,
//[Email] NVARCHAR (256) NULL,
//[EmailConfirmed] BIT NOT NULL,
//[PasswordHash] NVARCHAR (MAX) NULL,
//[SecurityStamp] NVARCHAR (MAX) NULL,
//[PhoneNumber] NVARCHAR (MAX) NULL,
//[PhoneNumberConfirmed] BIT NOT NULL,
//[TwoFactorEnabled] BIT NOT NULL,
//[LockoutEndDateUtc] DATETIME NULL,
//[LockoutEnabled] BIT NOT NULL,
//[AccessFailedCount] INT NOT NULL,
//[UserName] NVARCHAR (256) NOT NULL
//) ";
// //CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
// command.ExecuteNonQuery();
// }
// }
// }
//#else
// throw new NotSupportedException("SQL Server is not yet supported when running against K10.");
//#endif
//}
// TODO: CreateAsync DB?
return db;
}
public static UserManager<EntityUser> CreateManager(EntityContext context)
{
return new UserManager<EntityUser>(new UserStore(context));
}
public static UserManager<EntityUser> CreateManager()
{
return CreateManager(CreateContext());
}
public static RoleManager<EntityRole> CreateRoleManager(EntityContext context)
{
return new RoleManager<EntityRole>(new RoleStore<EntityRole, string>(context));
}
public static RoleManager<EntityRole> CreateRoleManager()
{
return CreateRoleManager(CreateContext());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -50,5 +50,6 @@
}
},
"commands": {
"test": "Xunit.KRunner"
}
}

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
var helper = new SignInManager<TestUser> { UserManager = userManager, AuthenticationType = authType, Context = context.Object };
// Act
await helper.SignIn(user, false, false);
await helper.SignInAsync(user, false, false);
// Assert
identityFactory.VerifyAll();
@ -65,7 +65,7 @@ namespace Microsoft.AspNet.Identity.Security.Test
manager.Setup(m => m.IsLockedOutAsync(user, CancellationToken.None)).ReturnsAsync(false).Verifiable();
manager.Setup(m => m.FindByNameAsync(user.UserName, CancellationToken.None)).ReturnsAsync(user).Verifiable();
manager.Setup(m => m.CheckPasswordAsync(user, "password", CancellationToken.None)).ReturnsAsync(true).Verifiable();
manager.Setup(m => m.CreateIdentity(user, "Microsoft.AspNet.Identity", CancellationToken.None)).ReturnsAsync(new ClaimsIdentity("Microsoft.AspNet.Identity")).Verifiable();
manager.Setup(m => m.CreateIdentityAsync(user, "Microsoft.AspNet.Identity", CancellationToken.None)).ReturnsAsync(new ClaimsIdentity("Microsoft.AspNet.Identity")).Verifiable();
var context = new Mock<HttpContext>();
var response = new Mock<HttpResponse>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
@ -129,6 +129,16 @@ namespace Microsoft.AspNet.Identity.Security.Test
Assert.Equal(SignInStatus.Failure, result);
}
[Fact]
public async Task SignInWithNoContextDoesNotBlowUp()
{
// Setup
var helper = new SignInManager<TestUser>();
// Act
await helper.SignInAsync(null, false, false);
}
[Fact]
public async Task CreateUserIdentityReturnsNullNoUserManager()
{

View File

@ -53,7 +53,7 @@ namespace Microsoft.AspNet.Identity.Test
// Assert
Assert.NotNull(identity);
Assert.Equal(authType, identity.AuthenticationType);
var claims = identity.Claims;
var claims = identity.Claims.ToList();
Assert.NotNull(claims);
Assert.True(
claims.Any(c => c.Type == factory.UserNameClaimType && c.Value == user.UserName));
@ -66,48 +66,5 @@ namespace Microsoft.AspNet.Identity.Test
}
}
#endif
//[Fact]
//public async Task ClaimsIdentityTest()
//{
// var db = UnitTestHelper.CreateDefaultDb();
// var manager = new UserManager<TestUser>(new UserStore<TestUser>(db));
// var role = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
// var user = new TestUser("Hao");
// UnitTestHelper.IsSuccess(await manager.CreateAsync(user));
// UnitTestHelper.IsSuccess(await role.CreateAsync(new IdentityRole("Admin")));
// UnitTestHelper.IsSuccess(await role.CreateAsync(new IdentityRole("Local")));
// UnitTestHelper.IsSuccess(await manager.AddToRoleAsync(user.Id, "Admin"));
// UnitTestHelper.IsSuccess(await manager.AddToRoleAsync(user.Id, "Local"));
// Claim[] userClaims =
// {
// new Claim("Whatever", "Value"),
// new Claim("Whatever2", "Value2")
// };
// foreach (var c in userClaims)
// {
// UnitTestHelper.IsSuccess(await manager.AddClaimAsync(user.Id, c));
// }
// var identity = await manager.CreateIdentityAsync(user, "test");
// var claimsFactory = manager.ClaimsIdentityFactory as ClaimsIdentityFactory<TestUser, string>;
// Assert.NotNull(claimsFactory);
// var claims = identity.Claims;
// Assert.NotNull(claims);
// Assert.True(
// claims.Any(c => c.Type == claimsFactory.UserNameClaimType && c.Value == user.UserName));
// Assert.True(claims.Any(c => c.Type == claimsFactory.UserIdClaimType && c.Value == user.Id));
// Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Admin"));
// Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Local"));
// Assert.True(
// claims.Any(
// c =>
// c.Type == ClaimsIdentityFactory<TestUser>.IdentityProviderClaimType &&
// c.Value == ClaimsIdentityFactory<TestUser>.DefaultIdentityProviderClaimValue));
// foreach (var cl in userClaims)
// {
// Assert.True(claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
// }
//}
}
}

View File

@ -20,6 +20,11 @@ namespace Microsoft.AspNet.Identity.Test
return Task.FromResult<string>(null);
}
public Task SetRoleNameAsync(TestRole role, string roleName, CancellationToken cancellationToken = new CancellationToken())
{
return Task.FromResult(0);
}
public Task<TestRole> FindByIdAsync(string roleId, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult<TestRole>(null);

View File

@ -39,7 +39,7 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await manager.CreateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await manager.UpdateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("role", async () => await manager.DeleteAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("roleName", async () => await manager.FindByName(null));
await Assert.ThrowsAsync<ArgumentNullException>("roleName", async () => await manager.FindByNameAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("roleName", async () => await manager.RoleExistsAsync(null));
}
@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Identity.Test
var manager = new RoleManager<TestRole>(new NoopRoleStore());
manager.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByName(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByNameAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RoleExistsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.CreateAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.UpdateAsync(null));
@ -83,6 +83,11 @@ namespace Microsoft.AspNet.Identity.Test
throw new NotImplementedException();
}
public Task SetRoleNameAsync(TestRole role, string roleName, CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}
public Task<TestRole> FindByIdAsync(string roleId, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();

View File

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using System.Collections.Generic;
namespace Microsoft.AspNet.Identity.Test
{

View File

@ -1,12 +1,11 @@
using Microsoft.AspNet.DependencyInjection.Fallback;
using Moq;
using System;
using System.Collections.Generic;
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;
namespace Microsoft.AspNet.Identity.Test
@ -15,7 +14,7 @@ namespace Microsoft.AspNet.Identity.Test
{
private class TestManager : UserManager<TestUser>
{
public IUserStore<TestUser> StorePublic { get { return base.Store; } }
public IUserStore<TestUser> StorePublic { get { return Store; } }
public TestManager(IServiceProvider provider) : base(provider) { }
}
@ -102,7 +101,7 @@ namespace Microsoft.AspNet.Identity.Test
userManager.UserValidator = validator.Object;
// Act
var result = await userManager.SetUserName(user, "foo");
var result = await userManager.SetUserNameAsync(user, "foo");
// Assert
Assert.True(result.Succeeded);
@ -156,7 +155,7 @@ namespace Microsoft.AspNet.Identity.Test
public async Task FindWithUnknownUserAndPasswordReturnsNull()
{
var manager = new UserManager<TestUser>(new EmptyStore());
Assert.Null(await manager.FindByUserNamePassword("bogus", "whatevs"));
Assert.Null(await manager.FindByUserNamePasswordAsync("bogus", "whatevs"));
}
[Fact]
@ -219,12 +218,14 @@ namespace Microsoft.AspNet.Identity.Test
Assert.False(manager.SupportsUserSecurityStamp);
await Assert.ThrowsAsync<NotSupportedException>(() => manager.UpdateSecurityStampAsync(null));
await Assert.ThrowsAsync<NotSupportedException>(() => manager.GetSecurityStampAsync(null));
#if NET45
await
Assert.ThrowsAsync<NotSupportedException>(
() => manager.VerifyChangePhoneNumberTokenAsync(null, "1", "111-111-1111"));
await
Assert.ThrowsAsync<NotSupportedException>(
() => manager.GenerateChangePhoneNumberToken(null, "111-111-1111"));
() => manager.GenerateChangePhoneNumberTokenAsync(null, "111-111-1111"));
#endif
}
[Fact]
@ -232,9 +233,9 @@ namespace Microsoft.AspNet.Identity.Test
{
var manager = new UserManager<TestUser>(new NoopUserStore());
Assert.False(manager.SupportsUserLogin);
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.AddLogin(null, null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.RemoveLogin(null, null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.GetLogins(null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.AddLoginAsync(null, null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.RemoveLoginAsync(null, null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.GetLoginsAsync(null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.FindByLoginAsync(null));
}
@ -253,7 +254,7 @@ namespace Microsoft.AspNet.Identity.Test
{
var manager = new UserManager<TestUser>(new NoopUserStore());
Assert.False(manager.SupportsUserTwoFactor);
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.GetTwoFactorEnabled(null));
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.GetTwoFactorEnabledAsync(null));
await
Assert.ThrowsAsync<NotSupportedException>(async () => await manager.SetTwoFactorEnabledAsync(null, true));
}
@ -314,7 +315,7 @@ namespace Microsoft.AspNet.Identity.Test
Assert.Throws<ArgumentNullException>(() => manager.PasswordHasher = null);
await
Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.CreateIdentity(null, "whatever"));
async () => await manager.CreateIdentityAsync(null, "whatever"));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await manager.CreateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await manager.CreateAsync(null, null));
await
@ -324,10 +325,10 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await manager.DeleteAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("claim", async () => await manager.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("userName", async () => await manager.FindByNameAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("userName", async () => await manager.FindByUserNamePassword(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("login", async () => await manager.AddLogin(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("userName", async () => await manager.FindByUserNamePasswordAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("login", async () => await manager.AddLoginAsync(null, null));
await
Assert.ThrowsAsync<ArgumentNullException>("login", async () => await manager.RemoveLogin(null, null));
Assert.ThrowsAsync<ArgumentNullException>("login", async () => await manager.RemoveLoginAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("email", async () => await manager.FindByEmailAsync(null));
Assert.Throws<ArgumentNullException>("twoFactorProvider",
() => manager.RegisterTwoFactorProvider(null, null));
@ -341,10 +342,14 @@ namespace Microsoft.AspNet.Identity.Test
{
UserTokenProvider = new NoOpTokenProvider()
};
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetUserNameAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SetUserNameAsync(null, "bogus"));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.AddClaimAsync(null, new Claim("a", "b")));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.AddLogin(null, new UserLoginInfo("", "")));
async () => await manager.AddLoginAsync(null, new UserLoginInfo("", "")));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.AddPasswordAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
@ -354,7 +359,7 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetClaimsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetLogins(null));
async () => await manager.GetLoginsAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetRolesAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
@ -362,7 +367,7 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.RemoveClaimAsync(null, new Claim("a", "b")));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.RemoveLogin(null, new UserLoginInfo("", "")));
async () => await manager.RemoveLoginAsync(null, new UserLoginInfo("", "")));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.RemovePasswordAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
@ -398,7 +403,7 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SetPhoneNumberAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetTwoFactorEnabled(null));
async () => await manager.GetTwoFactorEnabledAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SetTwoFactorEnabledAsync(null, true));
await Assert.ThrowsAsync<ArgumentNullException>("user",
@ -408,7 +413,7 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.NotifyTwoFactorTokenAsync(null, null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetValidTwoFactorProviders(null));
async () => await manager.GetValidTwoFactorProvidersAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.VerifyUserTokenAsync(null, null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
@ -422,11 +427,15 @@ namespace Microsoft.AspNet.Identity.Test
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SetLockoutEnabledAsync(null, false));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SetLockoutEndDate(null, DateTimeOffset.UtcNow));
async () => await manager.SetLockoutEndDateAsync(null, DateTimeOffset.UtcNow));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.GetLockoutEndDate(null));
async () => await manager.GetLockoutEndDateAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.IsLockedOutAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SendEmailAsync(null, null, null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await manager.SendSmsAsync(null, null));
}
[Fact]
@ -436,26 +445,26 @@ namespace Microsoft.AspNet.Identity.Test
manager.Dispose();
Assert.Throws<ObjectDisposedException>(() => manager.ClaimsIdentityFactory);
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AddClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AddLogin(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AddLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AddPasswordAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AddToRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.ChangePasswordAsync(null, null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.GetClaimsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.GetLogins(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.GetLoginsAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.GetRolesAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.IsInRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemoveLogin(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemoveLoginAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemovePasswordAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemoveFromRoleAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.RemoveClaimAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByUserNamePassword(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByUserNamePasswordAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByLoginAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByIdAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.FindByNameAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.CreateAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.CreateAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.CreateIdentity(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.CreateIdentityAsync(null, null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.UpdateAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.DeleteAsync(null));
await Assert.ThrowsAsync<ObjectDisposedException>(() => manager.UpdateSecurityStampAsync(null));