Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Feeback: Added predicate for user/role verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas Joshi committed Mar 23, 2015
1 parent 42250ca commit 90cf9fe
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public static IServiceCollection ConfigureIdentityApplicationCookie(this IServic
return services.Configure<CookieAuthenticationOptions>(configureOptions, IdentityOptions.ApplicationCookieAuthenticationScheme);
}

public static IdentityBuilder AddIdentity(this IServiceCollection services)
{
return services.AddIdentity<IdentityUser, IdentityRole>(configureOptions: null);
}

public static IdentityBuilder AddIdentity<TUser, TRole>(
this IServiceCollection services)
where TUser : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.AspNet.Identity.Test;
using Microsoft.Framework.DependencyInjection;

Expand Down Expand Up @@ -50,5 +51,13 @@ protected override void SetUserPasswordHash(IdentityUser user, string hashedPass
{
user.PasswordHash = hashedPassword;
}

protected override Expression<Func<IdentityUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<IdentityUser, bool>>)(u => u.UserName.Contains(userName))
: (u => u.UserName == userName);

protected override Expression<Func<IdentityRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<IdentityRole, bool>>)(u => u.Name.Contains(roleName))
: (u => u.Name == roleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public async Task CanIncludeUserClaimsTest()
CreateContext(true);
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

builder.UseServices(services =>
{
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
});
var services = new ServiceCollection();
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
builder.ApplicationServices = services.BuildServiceProvider();

var userManager = builder.ApplicationServices.GetRequiredService<UserManager<IdentityUser>>();
var dbContext = builder.ApplicationServices.GetRequiredService<IdentityDbContext>();
Expand Down Expand Up @@ -104,11 +103,10 @@ public async Task CanIncludeUserLoginsTest()
CreateContext(true);
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

builder.UseServices(services =>
{
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
});
var services = new ServiceCollection();
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
builder.ApplicationServices = services.BuildServiceProvider();

var userManager = builder.ApplicationServices.GetRequiredService<UserManager<IdentityUser>>();
var dbContext = builder.ApplicationServices.GetRequiredService<IdentityDbContext>();
Expand All @@ -119,7 +117,7 @@ public async Task CanIncludeUserLoginsTest()

for (var i = 0; i < 10; i++)
{
IdentityResultAssert.IsSuccess(await userManager.AddLoginAsync(user, new UserLoginInfo("foo" + i, "bar" + i, "foo")));
IdentityResultAssert.IsSuccess(await userManager.AddLoginAsync(user, new UserLoginInfo("foo" + i, "bar" + i, "foo")));
}

user = dbContext.Users.Include(x => x.Logins).FirstOrDefault(x => x.UserName == username);
Expand All @@ -137,11 +135,10 @@ public async Task CanIncludeUserRolesTest()
CreateContext(true);
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

builder.UseServices(services =>
{
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
});
var services = new ServiceCollection();
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
builder.ApplicationServices = services.BuildServiceProvider();

var userManager = builder.ApplicationServices.GetRequiredService<UserManager<IdentityUser>>();
var roleManager = builder.ApplicationServices.GetRequiredService<RoleManager<IdentityRole>>();
Expand Down Expand Up @@ -184,11 +181,10 @@ public async Task CanIncludeRoleClaimsTest()
CreateContext(true);
var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

builder.UseServices(services =>
{
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
});
var services = new ServiceCollection();
DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
builder.ApplicationServices = services.BuildServiceProvider();

var roleManager = builder.ApplicationServices.GetRequiredService<RoleManager<IdentityRole>>();
var dbContext = builder.ApplicationServices.GetRequiredService<IdentityDbContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.AspNet.Identity.Test;
using Microsoft.Framework.DependencyInjection;
using Xunit;
Expand Down Expand Up @@ -68,5 +69,13 @@ protected override void AddRoleStore(IServiceCollection services, object context
{
services.AddInstance<IRoleStore<GuidRole>>(new ApplicationRoleStore((TestDbContext)context));
}

protected override Expression<Func<GuidUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<GuidUser, bool>>) (u => u.UserName.Contains(userName))
: (u => u.UserName == userName);

protected override Expression<Func<GuidRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<GuidRole, bool>>) (u => u.Name.Contains(roleName))
: (u => u.Name == roleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Xunit;

namespace Microsoft.AspNet.Identity.EntityFramework.Test
Expand Down Expand Up @@ -34,5 +35,13 @@ public override string ConnectionString
return _connectionString;
}
}

protected override Expression<Func<IntRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<IntRole, bool>>)(u => u.Name.Contains(roleName))
: (u => u.Name == roleName);

protected override Expression<Func<IntUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<IntUser, bool>>)(u => u.UserName.Contains(userName))
: (u => u.UserName == userName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Xunit;

namespace Microsoft.AspNet.Identity.EntityFramework.Test
Expand Down Expand Up @@ -37,5 +38,13 @@ public override string ConnectionString
return _connectionString;
}
}

protected override Expression<Func<StringRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<StringRole, bool>>)(u => u.Name.Contains(roleName))
: (u => u.Name == roleName);

protected override Expression<Func<StringUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<StringUser, bool>>)(u => u.UserName.Contains(userName))
: (u => u.UserName == userName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
Expand Down Expand Up @@ -456,6 +457,13 @@ protected override void SetUserPasswordHash(IdentityUser user, string hashedPass
user.PasswordHash = hashedPassword;
}

protected override Expression<Func<IdentityUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<IdentityUser, bool>>)(u => u.UserName.Contains(userName))
: (u => u.UserName == userName);

protected override Expression<Func<IdentityRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<IdentityRole, bool>>)(u => u.Name.Contains(roleName))
: (u => u.Name == roleName);
}

public class ApplicationUser : IdentityUser
Expand Down
13 changes: 7 additions & 6 deletions test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Framework.DependencyInjection;
using Shouldly;
using Xunit;
using Microsoft.AspNet.Identity.Test;

namespace Microsoft.AspNet.Identity.InMemory
{
Expand Down Expand Up @@ -162,16 +163,16 @@ private static TestServer CreateServer(Action<CookieAuthenticationOptions> confi
{
var req = context.Request;
var res = context.Response;
var userManager = context.RequestServices.GetRequiredService<UserManager<InMemoryUser>>();
var signInManager = context.RequestServices.GetRequiredService<SignInManager<InMemoryUser>>();
var userManager = context.RequestServices.GetRequiredService<UserManager<TestUser>>();
var signInManager = context.RequestServices.GetRequiredService<SignInManager<TestUser>>();
PathString remainder;
if (req.Path == new PathString("/normal"))
{
res.StatusCode = 200;
}
else if (req.Path == new PathString("/createMe"))
{
var result = await userManager.CreateAsync(new InMemoryUser("hao"), TestPassword);
var result = await userManager.CreateAsync(new TestUser("hao"), TestPassword);
res.StatusCode = result.Succeeded ? 200 : 500;
}
else if (req.Path == new PathString("/protected"))
Expand Down Expand Up @@ -220,9 +221,9 @@ private static TestServer CreateServer(Action<CookieAuthenticationOptions> confi
},
services =>
{
services.AddIdentity<InMemoryUser, IdentityRole>();
services.AddSingleton<IUserStore<InMemoryUser>, InMemoryUserStore<InMemoryUser>>();
services.AddSingleton<IRoleStore<IdentityRole>, InMemoryRoleStore<IdentityRole>>();
services.AddIdentity<TestUser, TestRole>();
services.AddSingleton<IUserStore<TestUser>, InMemoryUserStore<TestUser>>();
services.AddSingleton<IRoleStore<TestRole>, InMemoryRoleStore<TestRole>>();
services.ConfigureIdentityApplicationCookie(configureAppCookie);
});
server.BaseAddress = baseAddress;
Expand Down
14 changes: 12 additions & 2 deletions test/Microsoft.AspNet.Identity.InMemory.Test/InMemoryStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.AspNet.Identity.Test;
using Microsoft.Framework.DependencyInjection;

Expand Down Expand Up @@ -36,17 +37,26 @@ protected override void SetUserPasswordHash(TestUser user, string hashedPassword
user.PasswordHash = hashedPassword;
}

protected override TestUser CreateTestUser(string namePrefix = "", string email = "", string phoneNumber = "",
protected override TestUser CreateTestUser(string namePrefix = "", string email = "", string phoneNumber = "",
bool lockoutEnabled = false, DateTimeOffset? lockoutEnd = default(DateTimeOffset?), bool useNamePrefixAsUserName = false)
{
return new TestUser
{
UserName = useNamePrefixAsUserName ? namePrefix : string.Format("{0}{1}", namePrefix, Guid.NewGuid()),
UserName = useNamePrefixAsUserName ? namePrefix : string.Format("{0}{1}", namePrefix, Guid.NewGuid()),
Email = email,
PhoneNumber = phoneNumber,
LockoutEnabled = lockoutEnabled,
LockoutEnd = lockoutEnd
};
}

protected override Expression<Func<TestUser, bool>> UserNamePredicate(string userName, bool contains = false)
=> contains ? (Expression<Func<TestUser, bool>>)(u => u.UserName.Contains(userName))
: (u => u.UserName == userName);

protected override Expression<Func<TestRole, bool>> RoleNamePredicate(string roleName, bool contains = false)
=> contains ? (Expression<Func<TestRole, bool>>)(u => u.Name.Contains(roleName))
: (u => u.Name == roleName);

}
}
2 changes: 1 addition & 1 deletion test/Microsoft.AspNet.Identity.Test/IdentityOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void CanSetupIdentityOptions()
var services = new ServiceCollection()
.AddOptions()
.ConfigureIdentity(options => options.User.RequireUniqueEmail = true);
services.AddIdentity();
services.AddIdentity<TestUser,TestRole>();
var serviceProvider = services.BuildServiceProvider();

var optionsGetter = serviceProvider.GetRequiredService<IOptions<IdentityOptions>>();
Expand Down
8 changes: 4 additions & 4 deletions test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ public async Task PasswordMethodsFailWhenStoreNotImplemented()
[Fact]
public async Task SecurityStampMethodsFailWhenStoreNotImplemented()
{
var store = new Mock<IUserStore<IdentityUser>>();
store.Setup(x => x.GetUserIdAsync(It.IsAny<IdentityUser>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(Guid.NewGuid().ToString()));
var store = new Mock<IUserStore<TestUser>>();
store.Setup(x => x.GetUserIdAsync(It.IsAny<TestUser>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(Guid.NewGuid().ToString()));
var manager = MockHelpers.TestUserManager(store.Object);
Assert.False(manager.SupportsUserSecurityStamp);
await Assert.ThrowsAsync<NotSupportedException>(() => manager.UpdateSecurityStampAsync(null));
Expand Down Expand Up @@ -637,8 +637,8 @@ public async Task PasswordValidatorBlocksCreate()
[Fact]
public async Task ResetTokenCallNoopForTokenValueZero()
{
var user = new IdentityUser() { UserName = Guid.NewGuid().ToString()};
var store = new Mock<IUserLockoutStore<IdentityUser>>();
var user = new TestUser() { UserName = Guid.NewGuid().ToString()};
var store = new Mock<IUserLockoutStore<TestUser>>();
store.Setup(x => x.ResetAccessFailedCountAsync(user, It.IsAny<CancellationToken>())).Returns(() =>
{
throw new Exception();
Expand Down
Loading

0 comments on commit 90cf9fe

Please sign in to comment.