diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/RoleStore.cs b/src/Microsoft.AspNet.Identity.EntityFramework/RoleStore.cs index 9a5ae99264..5d295cd69a 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/RoleStore.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/RoleStore.cs @@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework public virtual Task GetRoleAggregate(Expression> 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> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken)) + public async Task> 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)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 { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); - return Task.FromResult(0); + + return RoleClaims.AddAsync(new IdentityRoleClaim { 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 Roles diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs b/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs index a4ff3efd45..685e360934 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/UserStore.cs @@ -74,11 +74,10 @@ namespace Microsoft.AspNet.Identity.EntityFramework protected virtual Task GetUserAggregate(Expression> 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 GetUserIdAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) @@ -279,7 +278,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework /// /// /// - 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 { 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); } /// @@ -311,7 +309,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework /// /// /// - 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); } /// @@ -342,7 +339,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework /// /// /// - public virtual async Task> GetRolesAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) + public virtual Task> 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>(query.ToList()); } /// @@ -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> UserRoles { get { return Context.Set>(); } } private DbSet> UserLogins { get { return Context.Set>(); } } - public Task> GetClaimsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) + public async Task> 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)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 claims, CancellationToken cancellationToken = default(CancellationToken)) + public async Task AddClaimsAsync(TUser user, IEnumerable 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 { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value }); + await UserClaims.AddAsync(new IdentityUserClaim { 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 claims, CancellationToken cancellationToken = default(CancellationToken)) + public async Task RemoveClaimsAsync(TUser user, IEnumerable 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> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) + public async virtual Task> GetLoginsAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); @@ -546,6 +536,7 @@ namespace Microsoft.AspNet.Identity.EntityFramework //IList 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);