本文整理汇总了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());
}
示例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>>();
}
示例3: CreateTestServices
public static IServiceCollection CreateTestServices()
{
var services = new ServiceCollection();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
services.AddIdentity<IdentityUser, IdentityRole>();
return services;
}
示例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();
}
示例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")));
}
示例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")));
}
示例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();
}
示例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();
}
示例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));
}
示例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();
}
示例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());
}
示例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());
}
}