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


C# UserManager.AddClaim方法代码示例

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


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

示例1: ClaimsIdentitySyncTest

 public void ClaimsIdentitySyncTest()
 {
     var db = UnitTestHelper.CreateDefaultDb();
     var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
     var role = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
     var user = new IdentityUser("Hao");
     var claimsFactory = manager.ClaimsIdentityFactory as ClaimsIdentityFactory<IdentityUser, string>;
     Assert.NotNull(claimsFactory);
     UnitTestHelper.IsSuccess(manager.Create(user));
     UnitTestHelper.IsSuccess(role.Create(new IdentityRole("Admin")));
     UnitTestHelper.IsSuccess(role.Create(new IdentityRole("Local")));
     UnitTestHelper.IsSuccess(manager.AddToRole(user.Id, "Admin"));
     UnitTestHelper.IsSuccess(manager.AddToRole(user.Id, "Local"));
     Claim[] userClaims =
     {
         new Claim("Whatever", "Value"),
         new Claim("Whatever2", "Value2")
     };
     foreach (var c in userClaims)
     {
         UnitTestHelper.IsSuccess(manager.AddClaim(user.Id, c));
     }
     var identity = manager.CreateIdentity(user, "test");
     var claims = identity.Claims;
     Assert.NotNull(claims);
     Assert.NotNull(claims);
     Assert.True(
         claims.Any(c => c.Type == claimsFactory.UserNameClaimType && c.Value == user.UserName));
     Assert.True(claims.Any(c => c.Type == claimsFactory.UserIdClaimType && c.Value == user.Id));
     Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Admin"));
     Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Local"));
     Assert.True(
         claims.Any(
             c =>
                 c.Type == ClaimsIdentityFactory<IdentityUser>.IdentityProviderClaimType &&
                 c.Value == ClaimsIdentityFactory<IdentityUser>.DefaultIdentityProviderClaimValue));
     foreach (var cl in userClaims)
     {
         Assert.True(claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
     }
 }
开发者ID:tomi85,项目名称:aspnetidentity,代码行数:41,代码来源:ClaimsIdentityFactoryTest.cs

示例2: GetUserClaimSyncTest

 public void GetUserClaimSyncTest()
 {
     var db = UnitTestHelper.CreateDefaultDb();
     var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
     var user = new IdentityUser("u1");
     var result = manager.Create(user);
     UnitTestHelper.IsSuccess(result);
     Assert.NotNull(user);
     var claims = new[]
     {
         new Claim("c1", "v1"),
         new Claim("c2", "v2"),
         new Claim("c3", "v3")
     };
     foreach (Claim c in claims)
     {
         UnitTestHelper.IsSuccess(manager.AddClaim(user.Id, c));
     }
     var userClaims = new List<Claim>(manager.GetClaims(user.Id));
     Assert.Equal(3, userClaims.Count);
     foreach (Claim c in claims)
     {
         Assert.True(userClaims.Exists(u => u.Type == c.Type && u.Value == c.Value));
     }
 }
开发者ID:tomi85,项目名称:aspnetidentity,代码行数:25,代码来源:UserClaimsTest.cs

示例3: TeacherEdit

 public ActionResult TeacherEdit(User input)
 {
     UserManager manager = new UserManager();
     User user = manager.findById(input.Id);
     if (ModelState.IsValid) {
         manager.AddClaim(input.Id, new Claim(ClaimTypes.Role, "teacher"));
         return RedirectToAction("Index");
     }
     return View(user);
 }
开发者ID:OzzieSoft,项目名称:SrPrjGrp2,代码行数:10,代码来源:AdministratorController.cs

示例4: CreateWithoutCommitingNHibernateTransactionShouldNotInsertRows

 public void CreateWithoutCommitingNHibernateTransactionShouldNotInsertRows()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     using (var ts = _session.BeginTransaction())
     {
         userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome1");
         var x = userManager.FindByEmail("[email protected]");
         roleManager.Create(new IdentityRole("Admin"));
         userManager.AddClaim(x.Id, new Claim("role", "admin"));
         userManager.AddClaim(x.Id, new Claim("role", "user"));
         userManager.AddToRole(x.Id, "Admin");
         userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     }
     var x2 = userManager.FindByEmail("[email protected]");
     Assert.IsNull(x2);
 }
开发者ID:dags20,项目名称:NHibernate.AspNet.Identity.MultiTenant,代码行数:17,代码来源:UserStoreTest.cs

示例5: CreateWithoutCommitingTransactionScopeShouldNotInsertRows

 public void CreateWithoutCommitingTransactionScopeShouldNotInsertRows()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew))
     {
         // session is not opened inside the scope so we need to enlist it manually
         ((System.Data.Common.DbConnection)_session.Connection).EnlistTransaction(System.Transactions.Transaction.Current);
         userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome1");
         var x = userManager.FindByEmail("[email protected]");
         roleManager.Create(new IdentityRole("Admin"));
         userManager.AddClaim(x.Id, new Claim("role", "admin"));
         userManager.AddClaim(x.Id, new Claim("role", "user"));
         userManager.AddToRole(x.Id, "Admin");
         userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     }
     var x2 = userManager.FindByEmail("[email protected]");
     Assert.IsNull(x2);
 }
开发者ID:dags20,项目名称:NHibernate.AspNet.Identity.MultiTenant,代码行数:19,代码来源:UserStoreTest.cs

示例6: FindByEmailAggregated

 public void FindByEmailAggregated()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome");
     var x = userManager.FindByEmail("[email protected]");
     roleManager.CreateAsync(new IdentityRole("Admin"));
     userManager.AddClaim(x.Id, new Claim("role", "admin"));
     userManager.AddClaim(x.Id, new Claim("role", "user"));
     userManager.AddToRole(x.Id, "Admin");
     userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     this._session.Clear();
     x = userManager.FindByEmail("[email protected]");
     Assert.IsNotNull(x);
     Assert.AreEqual(2, x.Claims.Count);
     Assert.AreEqual(1, x.Roles.Count);
     Assert.AreEqual(1, x.Logins.Count);
 }
开发者ID:dags20,项目名称:NHibernate.AspNet.Identity.MultiTenant,代码行数:18,代码来源:UserStoreTest.cs

示例7: AddClaim

 public void AddClaim()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var user = new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true };
     userManager.Create(user, "Welcome");
     userManager.AddClaim(user.Id, new Claim(ClaimTypes.Role, "Admin"));
     Assert.AreEqual(1, userManager.GetClaims(user.Id).Count());
 }
开发者ID:dags20,项目名称:NHibernate.AspNet.Identity.MultiTenant,代码行数:8,代码来源:UserStoreTest.cs


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