当前位置: 首页>>代码示例>>C#>>正文


C# ServiceCollection.AddIdentity方法代码示例

本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.AddIdentity方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddIdentity方法的具体用法?C# ServiceCollection.AddIdentity怎么用?C# ServiceCollection.AddIdentity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Framework.DependencyInjection.ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.AddIdentity方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CanIncludeUserClaimsTest

        public async Task CanIncludeUserClaimsTest()
        {
            // Arrange
            CreateContext(true);
            var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

            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>();

            var username = "user" + new Random().Next();
            var user = new IdentityUser() { UserName = username };
            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user));

            for (var i = 0; i < 10; i++)
            {
                IdentityResultAssert.IsSuccess(await userManager.AddClaimAsync(user, new Claim(i.ToString(), "foo")));
            }

            user = dbContext.Users.Include(x => x.Claims).FirstOrDefault(x => x.UserName == username);

            // Assert
            Assert.NotNull(user);
            Assert.NotNull(user.Claims);
            Assert.Equal(10, user.Claims.Count());
        }
开发者ID:stormwild,项目名称:Identity,代码行数:30,代码来源:DefaultPocoTest.cs

示例2: CreateRoleManager

 public static RoleManager<IdentityRole> CreateRoleManager(InMemoryContext context)
 {
     var services = new ServiceCollection();
     services.AddIdentity<IdentityUser, IdentityRole>();
     services.AddInstance<IRoleStore<IdentityRole>>(new RoleStore<IdentityRole>(context));
     return services.BuildServiceProvider().GetRequiredService<RoleManager<IdentityRole>>();
 }
开发者ID:stormwild,项目名称:Identity,代码行数:7,代码来源:TestIdentityFactory.cs

示例3: CreateTestServices

 public static IServiceCollection CreateTestServices()
 {
     var services = new ServiceCollection();
     services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
     services.AddLogging();
     services.AddIdentity<IdentityUser, IdentityRole>();
     return services;
 }
开发者ID:491134648,项目名称:Identity,代码行数:8,代码来源:TestIdentityFactory.cs

示例4: ConfigureServices

 private IServiceProvider ConfigureServices()
 {
     var services = new ServiceCollection();
     DbUtil.ConfigureDbServices<IdentityDbContext>(ConnectionString, services);
     services.AddLogging();
     services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
     return services.BuildServiceProvider();
 }
开发者ID:bmansoob,项目名称:Identity,代码行数:8,代码来源:DefaultPocoTest.cs

示例5: CanCreateUsingAddRoleManager

 public async Task CanCreateUsingAddRoleManager()
 {
     var services = new ServiceCollection();
     services.AddEntityFramework().AddInMemoryStore();
     var store = new RoleStore<IdentityRole>(new InMemoryContext());
     services.AddIdentity<InMemoryUser, IdentityRole>().AddRoleStore(() => store);
     var provider = services.BuildServiceProvider();
     var manager = provider.GetService<RoleManager<IdentityRole>>();
     Assert.NotNull(manager);
     IdentityResultAssert.IsSuccess(await manager.CreateAsync(new IdentityRole("arole")));
 }
开发者ID:ChMar,项目名称:Identity,代码行数:11,代码来源:RoleStoreTest.cs

示例6: CanCreateRoleWithSingletonManager

 public async Task CanCreateRoleWithSingletonManager()
 {
     var services = new ServiceCollection();
     services.AddEntityFramework().AddInMemoryStore();
     services.AddTransient<InMemoryContext>();
     services.AddTransient<IRoleStore<IdentityRole>, RoleStore<IdentityRole, InMemoryContext>>();
     services.AddIdentity<IdentityUser, IdentityRole>();
     services.AddSingleton<RoleManager<IdentityRole>>();
     var provider = services.BuildServiceProvider();
     var manager = provider.GetRequiredService<RoleManager<IdentityRole>>();
     Assert.NotNull(manager);
     IdentityResultAssert.IsSuccess(await manager.CreateAsync(new IdentityRole("someRole")));
 }
开发者ID:stormwild,项目名称:Identity,代码行数:13,代码来源:RoleStoreTest.cs

示例7: ManageControllerTest

        public ManageControllerTest()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework()
                    .AddInMemoryStore()
                    .AddDbContext<MusicStoreContext>();

            services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<MusicStoreContext>();

            // IHttpContextAccessor is required for SignInManager, and UserManager
            services.AddInstance<IHttpContextAccessor>(
                new HttpContextAccessor()
                    {
                        HttpContext = new TestHttpContext(),
                    });

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:alexandreana,项目名称:MusicStore,代码行数:19,代码来源:ManageControllerTest.cs

示例8: VerifyAccountControllerSignIn

        public async Task VerifyAccountControllerSignIn(bool isPersistent)
        {
            var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);
            app.UseCookieAuthentication();

            var context = new Mock<HttpContext>();
            var auth = new Mock<AuthenticationManager>();
            context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
            auth.Setup(a => a.SignInAsync(new IdentityCookieOptions().ApplicationCookieAuthenticationScheme,
                It.IsAny<ClaimsPrincipal>(),
                It.IsAny<AuthenticationProperties>())).Returns(Task.FromResult(0)).Verifiable();
            // REVIEW: is persistant mocking broken
            //It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Returns(Task.FromResult(0)).Verifiable();
            var contextAccessor = new Mock<IHttpContextAccessor>();
            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);
            var services = new ServiceCollection();
            services.AddLogging();
            services.AddInstance(contextAccessor.Object);
            services.AddIdentity<TestUser, TestRole>();
            services.AddSingleton<IUserStore<TestUser>, InMemoryUserStore<TestUser>>();
            services.AddSingleton<IRoleStore<TestRole>, InMemoryRoleStore<TestRole>>();
            app.ApplicationServices = services.BuildServiceProvider();

            // Act
            var user = new TestUser
            {
                UserName = "Yolo"
            };
            const string password = "[email protected]!";
            var userManager = app.ApplicationServices.GetRequiredService<UserManager<TestUser>>();
            var signInManager = app.ApplicationServices.GetRequiredService<SignInManager<TestUser>>();

            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));

            var result = await signInManager.PasswordSignInAsync(user, password, isPersistent, false);

            // Assert
            Assert.True(result.Succeeded);
            context.VerifyAll();
            auth.VerifyAll();
            contextAccessor.VerifyAll();
        }
开发者ID:491134648,项目名称:Identity,代码行数:42,代码来源:HttpSignInTest.cs

示例9: EnsureStartupUsageWorks

        public async Task EnsureStartupUsageWorks()
        {
            var context = CreateContext(true);
            var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

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

            var userStore = builder.ApplicationServices.GetRequiredService<IUserStore<IdentityUser>>();
            var userManager = builder.ApplicationServices.GetRequiredService<UserManager<IdentityUser>>();

            Assert.NotNull(userStore);
            Assert.NotNull(userManager);

            const string userName = "admin";
            const string password = "[email protected]";
            var user = new IdentityUser { UserName = userName };
            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
            IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user));
        }
开发者ID:stormwild,项目名称:Identity,代码行数:22,代码来源:DefaultPocoTest.cs

示例10: ManageControllerTest

        public ManageControllerTest()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework()
                    .AddInMemoryDatabase()
                    .AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());

            services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<MusicStoreContext>();

            services.AddLogging();

            // IHttpContextAccessor is required for SignInManager, and UserManager
            var context = new DefaultHttpContext();
            context.SetFeature<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = new TestAuthHandler() });
            services.AddInstance<IHttpContextAccessor>(
                new HttpContextAccessor()
                    {
                        HttpContext = context,
                    });

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:renattomachado,项目名称:MusicStore,代码行数:23,代码来源:ManageControllerTest.cs

示例11: CanIncludeRoleClaimsTest

        public async Task CanIncludeRoleClaimsTest()
        {
            // Arrange
            CreateContext(true);
            var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

            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>();

            var role = new IdentityRole("Admin");

            IdentityResultAssert.IsSuccess(await roleManager.CreateAsync(role));

            for (var i = 0; i < 10; i++)
            {
                IdentityResultAssert.IsSuccess(await roleManager.AddClaimAsync(role, new Claim("foo" + i, "bar" + i)));
            }

            role = dbContext.Roles.Include(x => x.Claims).FirstOrDefault(x => x.Name == "Admin");

            // Assert
            Assert.NotNull(role);
            Assert.NotNull(role.Claims);
            Assert.Equal(10, role.Claims.Count());
        }
开发者ID:stormwild,项目名称:Identity,代码行数:30,代码来源:DefaultPocoTest.cs

示例12: CanIncludeUserRolesTest

        public async Task CanIncludeUserRolesTest()
        {
            // Arrange
            CreateContext(true);
            var builder = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider);

            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>>();
            var dbContext = builder.ApplicationServices.GetRequiredService<IdentityDbContext>();

            const string roleName = "Admin";
            for (var i = 0; i < 10; i++)
            {
                IdentityResultAssert.IsSuccess(await roleManager.CreateAsync(new IdentityRole(roleName + i)));
            }
            var username = "user" + new Random().Next();
            var user = new IdentityUser() { UserName = username };
            IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user));

            for (var i = 0; i < 10; i++)
            {
                IdentityResultAssert.IsSuccess(await userManager.AddToRoleAsync(user, roleName + i));
            }

            user = dbContext.Users.Include(x => x.Roles).FirstOrDefault(x => x.UserName == username);

            // Assert
            Assert.NotNull(user);
            Assert.NotNull(user.Roles);
            Assert.Equal(10, user.Roles.Count());

            for (var i = 0; i < 10; i++)
            {
                var role = dbContext.Roles.Include(r => r.Users).FirstOrDefault(r => r.Name == (roleName + i));
                Assert.NotNull(role);
                Assert.NotNull(role.Users);
                Assert.Equal(1, role.Users.Count());
            }
        }
开发者ID:stormwild,项目名称:Identity,代码行数:44,代码来源:DefaultPocoTest.cs


注:本文中的Microsoft.Framework.DependencyInjection.ServiceCollection.AddIdentity方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。