public async Task EnsureStartupUsageWorks() { var context = CreateContext(true); IBuilder builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => { services.AddEntityFramework().AddSqlServer(); services.AddIdentitySqlServer(); services.SetupOptions<DbContextOptions>(options => options.UseSqlServer(ConnectionString)); // todo: constructor resolution doesn't work well with IdentityDbContext since it has 4 constructors services.AddInstance(context); }); var userStore = builder.ApplicationServices.GetService<IUserStore<IdentityUser>>(); var userManager = builder.ApplicationServices.GetService<UserManager<IdentityUser>>(); Assert.NotNull(userStore); Assert.NotNull(userManager); const string userName = "******"; const string password = "******"; var user = new IdentityUser { UserName = userName }; IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user)); }
public async Task EnsureStartupUsageWorks() { var context = CreateContext(true); IBuilder builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => { services.AddEntityFramework().AddSqlServer(); services.AddIdentitySqlServer(); services.SetupOptions <DbContextOptions>(options => options.UseSqlServer(ConnectionString)); // todo: constructor resolution doesn't work well with IdentityDbContext since it has 4 constructors services.AddInstance(context); }); var userStore = builder.ApplicationServices.GetService <IUserStore <IdentityUser> >(); var userManager = builder.ApplicationServices.GetService <UserManager <IdentityUser> >(); Assert.NotNull(userStore); Assert.NotNull(userManager); const string userName = "******"; const string password = "******"; var user = new IdentityUser { UserName = userName }; IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user)); }
public void VerifyUseInMemoryLifetimes() { var builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => services.AddIdentity <ApplicationUser>().AddInMemory()); var userStore = builder.ApplicationServices.GetService <IUserStore <ApplicationUser> >(); var roleStore = builder.ApplicationServices.GetService <IRoleStore <IdentityRole> >(); var userManager = builder.ApplicationServices.GetService <UserManager <ApplicationUser> >(); var roleManager = builder.ApplicationServices.GetService <RoleManager <IdentityRole> >(); Assert.NotNull(userStore); Assert.NotNull(userManager); Assert.NotNull(roleStore); Assert.NotNull(roleManager); var userStore2 = builder.ApplicationServices.GetService <IUserStore <ApplicationUser> >(); var roleStore2 = builder.ApplicationServices.GetService <IRoleStore <IdentityRole> >(); var userManager2 = builder.ApplicationServices.GetService <UserManager <ApplicationUser> >(); var roleManager2 = builder.ApplicationServices.GetService <RoleManager <IdentityRole> >(); // Stores are singleton, managers are scoped Assert.Equal(userStore, userStore2); Assert.Equal(userManager, userManager2); Assert.Equal(roleStore, roleStore2); Assert.Equal(roleManager, roleManager2); }
public void VerifyUseInMemoryLifetimes() { var builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => services.AddIdentity<ApplicationUser>().AddInMemory()); var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>(); var roleStore = builder.ApplicationServices.GetService<IRoleStore<IdentityRole>>(); var userManager = builder.ApplicationServices.GetService<UserManager<ApplicationUser>>(); var roleManager = builder.ApplicationServices.GetService<RoleManager<IdentityRole>>(); Assert.NotNull(userStore); Assert.NotNull(userManager); Assert.NotNull(roleStore); Assert.NotNull(roleManager); var userStore2 = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>(); var roleStore2 = builder.ApplicationServices.GetService<IRoleStore<IdentityRole>>(); var userManager2 = builder.ApplicationServices.GetService<UserManager<ApplicationUser>>(); var roleManager2 = builder.ApplicationServices.GetService<RoleManager<IdentityRole>>(); // Stores are singleton, managers are scoped Assert.Equal(userStore, userStore2); Assert.Equal(userManager, userManager2); Assert.Equal(roleStore, roleStore2); Assert.Equal(roleManager, roleManager2); }
public void CanCustomizeIdentityOptions() { var builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => { services.AddIdentity <IdentityUser>(); services.AddSetup <PasswordsNegativeLengthSetup>(); }); var setup = builder.ApplicationServices.GetService <IOptionsSetup <IdentityOptions> >(); Assert.IsType(typeof(PasswordsNegativeLengthSetup), setup); var optionsGetter = builder.ApplicationServices.GetService <IOptionsAccessor <IdentityOptions> >(); Assert.NotNull(optionsGetter); setup.Setup(optionsGetter.Options); var myOptions = optionsGetter.Options; Assert.True(myOptions.Password.RequireLowercase); Assert.True(myOptions.Password.RequireDigit); Assert.True(myOptions.Password.RequireNonLetterOrDigit); Assert.True(myOptions.Password.RequireUppercase); Assert.Equal(-1, myOptions.Password.RequiredLength); }
public void CanSetupIdentityOptions() { var app = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); app.UseServices(services => { services.AddIdentity <IdentityUser>().SetupOptions(options => options.User.RequireUniqueEmail = true); }); var optionsGetter = app.ApplicationServices.GetService <IOptionsAccessor <IdentityOptions> >(); Assert.NotNull(optionsGetter); var myOptions = optionsGetter.Options; Assert.True(myOptions.User.RequireUniqueEmail); }
public async Task EnsureStartupUsageWorks() { var builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => services.AddIdentity <ApplicationUser>().AddInMemory()); var userStore = builder.ApplicationServices.GetService <IUserStore <ApplicationUser> >(); var roleStore = builder.ApplicationServices.GetService <IRoleStore <IdentityRole> >(); var userManager = builder.ApplicationServices.GetService <UserManager <ApplicationUser> >(); var roleManager = builder.ApplicationServices.GetService <RoleManager <IdentityRole> >(); Assert.NotNull(userStore); Assert.NotNull(userManager); Assert.NotNull(roleStore); Assert.NotNull(roleManager); await CreateAdminUser(builder.ApplicationServices); }
public async Task EnsureStartupUsageWorks() { var builder = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); builder.UseServices(services => services.AddIdentity<ApplicationUser>().AddInMemory()); var userStore = builder.ApplicationServices.GetService<IUserStore<ApplicationUser>>(); var roleStore = builder.ApplicationServices.GetService<IRoleStore<IdentityRole>>(); var userManager = builder.ApplicationServices.GetService<UserManager<ApplicationUser>>(); var roleManager = builder.ApplicationServices.GetService<RoleManager<IdentityRole>>(); Assert.NotNull(userStore); Assert.NotNull(userManager); Assert.NotNull(roleStore); Assert.NotNull(roleManager); await CreateAdminUser(builder.ApplicationServices); }
public async Task VerifyAccountControllerSignIn(bool isPersistent) { IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType }); var context = new Mock <HttpContext>(); var response = new Mock <HttpResponse>(); context.Setup(c => c.Response).Returns(response.Object).Verifiable(); response.Setup(r => r.SignIn(It.IsAny <ClaimsIdentity>(), It.Is <AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable(); var contextAccessor = new Mock <IContextAccessor <HttpContext> >(); contextAccessor.Setup(a => a.Value).Returns(context.Object); app.UseServices(services => { services.AddInstance(contextAccessor.Object); services.AddIdentity <ApplicationUser, IdentityRole>().AddInMemory().AddAuthentication(); }); // Act var user = new ApplicationUser { UserName = "******" }; const string password = "******"; var userManager = app.ApplicationServices.GetService <UserManager <ApplicationUser> >(); var signInManager = app.ApplicationServices.GetService <SignInManager <ApplicationUser> >(); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false); // Assert Assert.Equal(SignInStatus.Success, result); context.VerifyAll(); response.VerifyAll(); contextAccessor.VerifyAll(); }
public async Task VerifyAccountControllerSignIn(bool isPersistent) { IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType }); var context = new Mock<HttpContext>(); var response = new Mock<HttpResponse>(); context.Setup(c => c.Response).Returns(response.Object).Verifiable(); response.Setup(r => r.SignIn(It.IsAny<ClaimsIdentity>(), It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable(); var contextAccessor = new Mock<IContextAccessor<HttpContext>>(); contextAccessor.Setup(a => a.Value).Returns(context.Object); app.UseServices(services => { services.AddInstance(contextAccessor.Object); services.AddIdentity<ApplicationUser, IdentityRole>().AddInMemory().AddAuthentication(); }); // Act var user = new ApplicationUser { UserName = "******" }; const string password = "******"; var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>(); var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>(); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false); // Assert Assert.Equal(SignInStatus.Success, result); context.VerifyAll(); response.VerifyAll(); contextAccessor.VerifyAll(); }