Updated to use EF Async methods

This commit is contained in:
Suhas Joshi 2014-11-13 15:21:05 -08:00
parent 191bf8815c
commit 42c809ca16
2 changed files with 38 additions and 48 deletions

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
public virtual Task<TRole> GetRoleAggregate(Expression<Func<TRole, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Roles.FirstOrDefault(filter));
return Roles.FirstOrDefaultAsync(filter);
}
public async virtual Task CreateAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
@ -195,15 +195,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework
_disposed = true;
}
public Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
public async Task<IList<Claim>> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (role == null)
{
throw new ArgumentNullException("role");
}
var result = RoleClaims.Where(rc => rc.RoleId.Equals(role.Id)).Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
return Task.FromResult((IList<Claim>)result);
return await RoleClaims.Where(rc => rc.RoleId.Equals(role.Id)).Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToListAsync();
}
public Task AddClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
@ -217,11 +217,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework
{
throw new ArgumentNullException("claim");
}
RoleClaims.Add(new IdentityRoleClaim<TKey> { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
return Task.FromResult(0);
return RoleClaims.AddAsync(new IdentityRoleClaim<TKey> { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
}
public Task RemoveClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
public async Task RemoveClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (role == null)
@ -232,12 +232,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework
{
throw new ArgumentNullException("claim");
}
var claims = RoleClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList();
var claims = await RoleClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync();
foreach (var c in claims)
{
RoleClaims.Remove(c);
}
return Task.FromResult(0);
}
public IQueryable<TRole> Roles

View File

@ -74,11 +74,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework
protected virtual Task<TUser> GetUserAggregate(Expression<Func<TUser, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(Users.FirstOrDefault(filter));
// TODO: return Users.FirstOrDefaultAsync(filter, cancellationToken);
//Include(u => u.Roles)
//.Include(u => u.Claims)
//.Include(u => u.Logins)
return Users.FirstOrDefaultAsync(filter, cancellationToken);
// TODO: .Include(u => u.Roles)
//.Include(u => u.Claims)
//.Include(u => u.Logins);
}
public Task<string> GetUserIdAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
@ -279,7 +278,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
public async virtual Task AddToRoleAsync(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
@ -291,17 +290,16 @@ namespace Microsoft.AspNet.Identity.EntityFramework
{
throw new ArgumentException(Resources.ValueCannotBeNullOrEmpty, "roleName");
}
var roleEntity = Roles.SingleOrDefault(r => r.Name.ToUpper() == roleName.ToUpper());
var roleEntity = await Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper());
if (roleEntity == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.RoleNotFound, roleName));
}
var ur = new IdentityUserRole<TKey> { UserId = user.Id, RoleId = roleEntity.Id };
// TODO: rely on fixup?
UserRoles.Add(ur);
await UserRoles.AddAsync(ur);
user.Roles.Add(ur);
roleEntity.Users.Add(ur);
return Task.FromResult(0);
}
/// <summary>
@ -311,7 +309,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
/// <param name="roleName"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Task RemoveFromRoleAsync(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
public async virtual Task RemoveFromRoleAsync(TUser user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
@ -323,17 +321,16 @@ namespace Microsoft.AspNet.Identity.EntityFramework
{
throw new ArgumentException(Resources.ValueCannotBeNullOrEmpty, "roleName");
}
var roleEntity = Roles.SingleOrDefault(r => r.Name.ToUpper() == roleName.ToUpper());
var roleEntity = await Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper());
if (roleEntity != null)
{
var userRole = UserRoles.FirstOrDefault(r => roleEntity.Id.Equals(r.RoleId) && r.UserId.Equals(user.Id));
var userRole = await UserRoles.FirstOrDefaultAsync(r => roleEntity.Id.Equals(r.RoleId) && r.UserId.Equals(user.Id));
if (userRole != null)
{
UserRoles.Remove(userRole);
user.Roles.Remove(userRole);
}
}
return Task.FromResult(0);
}
/// <summary>
@ -342,7 +339,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
/// <param name="user"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<IList<string>> GetRolesAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<IList<string>> GetRolesAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
@ -356,7 +353,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
join role in Roles on userRole.RoleId equals role.Id
select role.Name;
//return await query.ToListAsync();
return query.ToList();
return Task.FromResult<IList<string>>(query.ToList());
}
/// <summary>
@ -378,8 +375,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
{
throw new ArgumentException(Resources.ValueCannotBeNullOrEmpty, "roleName");
}
//var role = await Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper());
var role = Roles.SingleOrDefault(r => r.Name.ToUpper() == roleName.ToUpper());
var role = await Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper());
if (role != null)
{
var userId = user.Id;
@ -412,18 +408,18 @@ namespace Microsoft.AspNet.Identity.EntityFramework
private DbSet<IdentityUserRole<TKey>> UserRoles { get { return Context.Set<IdentityUserRole<TKey>>(); } }
private DbSet<IdentityUserLogin<TKey>> UserLogins { get { return Context.Set<IdentityUserLogin<TKey>>(); } }
public Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public async Task<IList<Claim>> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
var result = UserClaims.Where(uc => uc.UserId.Equals(user.Id)).Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
return Task.FromResult((IList<Claim>)result);
return await UserClaims.Where(uc => uc.UserId.Equals(user.Id)).Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToListAsync();
}
public Task AddClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken))
public async Task AddClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -436,12 +432,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework
}
foreach (var claim in claims)
{
UserClaims.Add(new IdentityUserClaim<TKey> { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
await UserClaims.AddAsync(new IdentityUserClaim<TKey> { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
}
return Task.FromResult(0);
}
public Task ReplaceClaimAsync(TUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default(CancellationToken))
public async Task ReplaceClaimAsync(TUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -457,17 +452,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework
throw new ArgumentNullException("newClaim");
}
var matchedClaims = UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList();
var matchedClaims = await UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync();
foreach(var matchedClaim in matchedClaims)
{
matchedClaim.ClaimValue = newClaim.Value;
matchedClaim.ClaimType = newClaim.Type;
}
return Task.FromResult(0);
}
public Task RemoveClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken))
public async Task RemoveClaimsAsync(TUser user, IEnumerable<Claim> claims, CancellationToken cancellationToken = default(CancellationToken))
{
ThrowIfDisposed();
if (user == null)
@ -479,16 +472,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework
throw new ArgumentNullException("claims");
}
foreach (var claim in claims) {
var matchedClaims = UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToList();
var matchedClaims = await UserClaims.Where(uc => uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync();
foreach (var c in matchedClaims)
{
UserClaims.Remove(c);
}
}
return Task.FromResult(0);
}
public virtual Task AddLoginAsync(TUser user, UserLoginInfo login,
public virtual async Task AddLoginAsync(TUser user, UserLoginInfo login,
CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
@ -509,12 +501,11 @@ namespace Microsoft.AspNet.Identity.EntityFramework
ProviderDisplayName = login.ProviderDisplayName
};
// TODO: fixup so we don't have to update both
UserLogins.Add(l);
await UserLogins.AddAsync(l);
user.Logins.Add(l);
return Task.FromResult(0);
}
public virtual Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
public virtual async Task RemoveLoginAsync(TUser user, string loginProvider, string providerKey,
CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
@ -525,16 +516,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework
}
var userId = user.Id;
// todo: ensure logins loaded
var entry = UserLogins.SingleOrDefault(l => l.UserId.Equals(userId) && l.LoginProvider == loginProvider && l.ProviderKey == providerKey);
var entry = await UserLogins.SingleOrDefaultAsync(l => l.UserId.Equals(userId) && l.LoginProvider == loginProvider && l.ProviderKey == providerKey);
if (entry != null)
{
UserLogins.Remove(entry);
user.Logins.Remove(entry);
}
return Task.FromResult(0);
}
public virtual async Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
public async virtual Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
@ -546,6 +536,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework
//IList<UserLoginInfo> result = user.Logins
// .Select(l => new UserLoginInfo(l.LoginProvider, l.ProviderKey, l.ProviderDisplayName)).ToList();
var userId = user.Id;
return await UserLogins.Where(l => l.UserId.Equals(userId))
.Select(l => new UserLoginInfo(l.LoginProvider, l.ProviderKey, l.ProviderDisplayName)).ToListAsync();
}
@ -556,8 +547,8 @@ namespace Microsoft.AspNet.Identity.EntityFramework
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
// todo: ensure logins loaded
var userLogin =
UserLogins.FirstOrDefault(l => l.LoginProvider == loginProvider && l.ProviderKey == providerKey);
var userLogin = await
UserLogins.FirstOrDefaultAsync(l => l.LoginProvider == loginProvider && l.ProviderKey == providerKey);
if (userLogin != null)
{
return await GetUserAggregate(u => u.Id.Equals(userLogin.UserId), cancellationToken);