本文整理汇总了C#中UserStore.AddToRoleAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserStore.AddToRoleAsync方法的具体用法?C# UserStore.AddToRoleAsync怎么用?C# UserStore.AddToRoleAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserStore
的用法示例。
在下文中一共展示了UserStore.AddToRoleAsync方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddToRoleAsync_GivenAUserAndRole_AddsTheUserToTheRole
public async void AddToRoleAsync_GivenAUserAndRole_AddsTheUserToTheRole()
{
var applicationDatabaseConfiguration = new ApplicationDatabaseConfiguration();
var userStore = new UserStore<User>(applicationDatabaseConfiguration);
var user = new User
{
Email = "[email protected]",
IsEmailConfirmed = true,
PasswordHash = "PasswordHash",
PhoneNumber = "PhoneNumber",
IsPhoneNumberConfirmed = true,
IsTwoFactorEnabled = false,
LockoutEndDateUtc = null,
IsLockoutEnabled = true,
AccessFailedCount = 0,
UserName = "UserName",
IsAccountActive = true
};
await userStore.CreateAsync(user);
await userStore.AddToRoleAsync(user, "Admin");
var roles = await userStore.GetRolesAsync(user);
roles.Should().HaveCount(1);
roles[0].Should().Be("Admin");
}
示例2: UserStoreMethodsThrowWhenDisposedTest
public void UserStoreMethodsThrowWhenDisposedTest()
{
var db = UnitTestHelper.CreateDefaultDb();
var store = new UserStore<IdentityUser>(db);
store.Dispose();
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddClaimAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddLoginAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddToRoleAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetClaimsAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetLoginsAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetRolesAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.IsInRoleAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveLoginAsync(null, null)));
Assert.Throws<ObjectDisposedException>(
() => AsyncHelper.RunSync(() => store.RemoveFromRoleAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(null, null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindByIdAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindByNameAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.UpdateAsync(null)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.DeleteAsync(null)));
Assert.Throws<ObjectDisposedException>(
() => AsyncHelper.RunSync(() => store.SetEmailConfirmedAsync(null, true)));
Assert.Throws<ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetEmailConfirmedAsync(null)));
Assert.Throws<ObjectDisposedException>(
() => AsyncHelper.RunSync(() => store.SetPhoneNumberConfirmedAsync(null, true)));
Assert.Throws<ObjectDisposedException>(
() => AsyncHelper.RunSync(() => store.GetPhoneNumberConfirmedAsync(null)));
}
示例3: UserStorePublicNullCheckTest
public void UserStorePublicNullCheckTest()
{
var store = new UserStore<IdentityUser>();
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.CreateAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.UpdateAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.DeleteAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.AddClaimAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(null, null)),
"user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetClaimsAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetLoginsAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetRolesAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.AddLoginAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.RemoveLoginAsync(null, null)),
"user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.AddToRoleAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.RemoveFromRoleAsync(null, null)),
"user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.IsInRoleAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetPasswordHashAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.SetPasswordHashAsync(null, null)),
"user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetSecurityStampAsync(null)),
"user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.SetSecurityStampAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.AddClaimAsync(new IdentityUser("fake"), null)), "claim");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(new IdentityUser("fake"), null)), "claim");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.AddLoginAsync(new IdentityUser("fake"), null)), "login");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.RemoveLoginAsync(new IdentityUser("fake"), null)), "login");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.FindAsync(null)), "login");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetEmailConfirmedAsync(null)),
"user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.SetEmailConfirmedAsync(null, true)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetEmailAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.SetEmailAsync(null, null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetPhoneNumberAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.SetPhoneNumberAsync(null, null)),
"user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.GetPhoneNumberConfirmedAsync(null)), "user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.SetPhoneNumberConfirmedAsync(null, true)), "user");
ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => store.GetTwoFactorEnabledAsync(null)),
"user");
ExceptionHelper.ThrowsArgumentNull(
() => AsyncHelper.RunSync(() => store.SetTwoFactorEnabledAsync(null, true)), "user");
ExceptionHelper.ThrowsArgumentNullOrEmpty(
() => AsyncHelper.RunSync(() => store.AddToRoleAsync(new IdentityUser("fake"), null)), "roleName");
ExceptionHelper.ThrowsArgumentNullOrEmpty(
() => AsyncHelper.RunSync(() => store.RemoveFromRoleAsync(new IdentityUser("fake"), null)), "roleName");
ExceptionHelper.ThrowsArgumentNullOrEmpty(
() => AsyncHelper.RunSync(() => store.IsInRoleAsync(new IdentityUser("fake"), null)), "roleName");
}
示例4: addRole
public async Task<ActionResult> addRole(string userID, string roleID)
{
ajaxReturnData data = new ajaxReturnData();
try
{
using (ApplicationDbContext DB = new ApplicationDbContext())
{
ApplicationUser user = new ApplicationUser();
var store = new UserStore<ApplicationUser>(DB);
UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(store);
user = um.FindById(userID);
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(DB));
IdentityRole role = RoleManager.FindById(roleID);
await store.AddToRoleAsync(user, role.Name);
await store.UpdateAsync(user);
data.statusCode = (int)statusCodes.successRun;
data.message = "Role '" + role.Name + "' added to user '" + user.UserName + "'";
}
}
catch(Exception ex)
{
data.statusCode = (int)statusCodes.fail;
data.message = "Failed to add role to user; Error is: "+ ex.Message;
}
return Json(data);
}
示例5: UpdateUser
public ActionResult UpdateUser(string Id, string UserName, string RoleName, string roleId)
{
try
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbEntity));
using (var store = new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
var userReloaded = store.Users.Where(u => u.Id == Id).FirstOrDefault();
userReloaded.Roles.Clear();
var result = store.AddToRoleAsync(userReloaded, RoleName);
var result2 = store.UpdateAsync(userReloaded);
if (result.IsCompleted && result2.IsCompleted)
{
store.Context.SaveChanges();
}
}
var userVm = new
{
Id = Id,
UserName = UserName,
RoleName = RoleName
};
return Json(new { Result = "OK", Data = userVm, Message = "Cambios Guardados." });
}
catch (Exception e)
{
return Json(new { Result = "ERROR", Message = e.Message });
}
}
示例6: IsInRoleAsync_GivenAUserAndARole_ReturnsTrueIfUserIsInRole
public async void IsInRoleAsync_GivenAUserAndARole_ReturnsTrueIfUserIsInRole()
{
var applicationDatabaseConfiguration = new ApplicationDatabaseConfiguration();
var userStore = new UserStore<User>(applicationDatabaseConfiguration);
var user = new User
{
Email = "[email protected]",
IsEmailConfirmed = true,
PasswordHash = "PasswordHash",
PhoneNumber = "PhoneNumber",
IsPhoneNumberConfirmed = true,
IsTwoFactorEnabled = false,
LockoutEndDateUtc = null,
IsLockoutEnabled = true,
AccessFailedCount = 0,
UserName = "UserName",
IsAccountActive = true
};
await userStore.CreateAsync(user);
await userStore.AddToRoleAsync(user, "User");
var isInRole = await userStore.IsInRoleAsync(user, "User");
isInRole.Should().BeTrue();
}
示例7: GetAllRoles
public void GetAllRoles()
{
var user1 = new IdentityUser("Lukz 04");
var user2 = new IdentityUser("Moa 01");
var user3 = new IdentityUser("Win 02");
var user4 = new IdentityUser("Andre 03");
var role = new IdentityRole("ADM");
var role2 = new IdentityRole("USR");
var store = new UserStore<IdentityUser>(this._session);
var roleStore = new RoleStore<IdentityRole>(this._session);
roleStore.CreateAsync(role);
roleStore.CreateAsync(role2);
store.CreateAsync(user1);
store.CreateAsync(user2);
store.CreateAsync(user3);
store.CreateAsync(user4);
store.AddToRoleAsync(user1, "ADM");
store.AddToRoleAsync(user2, "ADM");
store.AddToRoleAsync(user3, "ADM");
store.AddToRoleAsync(user4, "ADM");
store.AddToRoleAsync(user1, "USR");
store.AddToRoleAsync(user4, "USR");
Assert.IsTrue(this._session.Query<IdentityRole>().Any(x => x.Name == "ADM"));
Assert.IsTrue(this._session.Query<IdentityUser>().Any(x => x.UserName == "Lukz 04"));
Assert.IsTrue(this._session.Query<IdentityUser>().Any(x => x.UserName == "Andre 03"));
var resul = roleStore.Roles;
Assert.AreEqual(2, resul.Count());
}
示例8: WhenRemoveUserFromRole_ThenDoNotDeleteRole_BugFix
public void WhenRemoveUserFromRole_ThenDoNotDeleteRole_BugFix()
{
var user = new IdentityUser("Lukz 05");
var role = new IdentityRole("ADM05");
var store = new UserStore<IdentityUser>(_session);
var roleStore = new RoleStore<IdentityRole>(_session);
roleStore.CreateAsync(role);
store.CreateAsync(user);
store.AddToRoleAsync(user, "ADM05");
Assert.IsTrue(_session.Query<IdentityRole>().Any(x => x.Name == "ADM05"));
Assert.IsTrue(_session.Query<IdentityUser>().Any(x => x.UserName == "Lukz 05"));
Assert.IsTrue(store.IsInRoleAsync(user, "ADM05").Result);
var result = store.RemoveFromRoleAsync(user, "ADM05");
Assert.IsNull(result.Exception);
Assert.IsFalse(store.IsInRoleAsync(user, "ADM05").Result);
Assert.IsTrue(_session.Query<IdentityUser>().Any(x => x.UserName == "Lukz 05"));
Assert.IsTrue(_session.Query<IdentityRole>().Any(x => x.Name == "ADM05"));
}
示例9: GivenHaveRoles_WhenDeleteUser_ThenDeletingCausesNoCascade
public void GivenHaveRoles_WhenDeleteUser_ThenDeletingCausesNoCascade()
{
var user = new IdentityUser("Lukz 04");
var role = new IdentityRole("ADM");
var store = new UserStore<IdentityUser>(_session);
var roleStore = new RoleStore<IdentityRole>(_session);
roleStore.CreateAsync(role);
store.CreateAsync(user);
store.AddToRoleAsync(user, "ADM");
Assert.IsTrue(_session.Query<IdentityRole>().Any(x => x.Name == "ADM"));
Assert.IsTrue(_session.Query<IdentityUser>().Any(x => x.UserName == "Lukz 04"));
var result = store.DeleteAsync(user);
Assert.IsNull(result.Exception);
Assert.IsFalse(this._session.Query<IdentityUser>().Any(x => x.UserName == "Lukz 04"));
Assert.IsTrue(this._session.Query<IdentityRole>().Any(x => x.Name == "ADM"));
}