Beispiel #1
0
        public async Task CreateAsync_should_set_token()
        {
            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddDbContext <IdentityDbContext <IdentityUser> >(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString()))
                           .AddIdentity <IdentityUser, IdentityRole>()
                           .AddEntityFrameworkStores <IdentityDbContext <IdentityUser> >()
                           .Services.BuildServiceProvider();

            var userManager = provider.GetRequiredService <UserManager <IdentityUser> >();
            var context     = provider.GetRequiredService <IdentityDbContext <IdentityUser> >();
            var sut         = new IdentityUserTokenStore <IdentityUser>(userManager, context, provider.GetRequiredService <ILogger <IdentityUserTokenStore <IdentityUser> > >());

            var id   = Guid.NewGuid().ToString();
            var user = new IdentityUser
            {
                Email    = "*****@*****.**",
                Id       = id,
                UserName = id
            };
            await userManager.CreateAsync(user).ConfigureAwait(false);

            await sut.CreateAsync(new UserToken
            {
                UserId        = id,
                LoginProvider = id,
                Name          = id,
                Value         = id
            } as object).ConfigureAwait(false);

            Assert.NotNull(await userManager.GetAuthenticationTokenAsync(user, id, id).ConfigureAwait(false));
        }
Beispiel #2
0
        public async Task CreateAsync_should_return_token_id()
        {
            using var documentStore = new RavenDbTestDriverWrapper().GetDocumentStore();
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddRavenDbStores(p => documentStore);

            IServiceProvider provider = services.AddIdentityServer4AdminRavenDbkStores <IdentityUser, IdentityRole>(p => documentStore).BuildServiceProvider();

            using var scope = provider.CreateScope();
            provider        = scope.ServiceProvider;

            var userManager = provider.GetRequiredService <UserManager <IdentityUser> >();
            var user        = new IdentityUser
            {
                Id             = Guid.NewGuid().ToString(),
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                UserName       = Guid.NewGuid().ToString()
            };
            var userResult = await userManager.CreateAsync(user);

            Assert.True(userResult.Succeeded);

            var sut    = new IdentityUserTokenStore <IdentityUser>(userManager, new ScopedAsynDocumentcSession(documentStore.OpenAsyncSession()), provider.GetRequiredService <ILogger <IdentityUserTokenStore <IdentityUser> > >());
            var result = await sut.CreateAsync(new Entity.UserToken
            {
                UserId        = user.Id,
                LoginProvider = Guid.NewGuid().ToString(),
                Name          = Guid.NewGuid().ToString(),
                Value         = Guid.NewGuid().ToString()
            } as object);


            Assert.NotNull(result);
            Assert.NotNull(((Entity.UserToken)result).Id);
        }
Beispiel #3
0
        public async Task CreateAsync_should_throw_on_user_not_found()
        {
            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddDbContext <IdentityDbContext <IdentityUser> >(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString()))
                           .AddIdentity <IdentityUser, IdentityRole>()
                           .AddEntityFrameworkStores <IdentityDbContext <IdentityUser> >()
                           .Services.BuildServiceProvider();

            var userManager = provider.GetRequiredService <UserManager <IdentityUser> >();
            var context     = provider.GetRequiredService <IdentityDbContext <IdentityUser> >();
            var sut         = new IdentityUserTokenStore <IdentityUser>(userManager, context, provider.GetRequiredService <ILogger <IdentityUserTokenStore <IdentityUser> > >());

            var id = Guid.NewGuid().ToString();
            await Assert.ThrowsAsync <IdentityException>(() => sut.CreateAsync(new UserToken
            {
                UserId        = id,
                LoginProvider = id,
                Name          = id,
                Value         = id
            })).ConfigureAwait(false);
        }