本文整理汇总了C#中UserManager.RemoveFromRoleAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.RemoveFromRoleAsync方法的具体用法?C# UserManager.RemoveFromRoleAsync怎么用?C# UserManager.RemoveFromRoleAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.RemoveFromRoleAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RemoveAdminFromUser
public IHttpActionResult RemoveAdminFromUser(string username)
{
var user = this.Data.Users.All().ToList().FirstOrDefault(u => u.UserName == username);
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
string adminCheck = um.GetRoles(user.Id).FirstOrDefault(a => a.Contains("AppAdmin"));
if (adminCheck == string.Empty)
{
return this.BadRequest(string.Format("user {0} is not admin", user.UserName));
}
um.RemoveFromRoleAsync(user.Id, "AppAdmin");
this.Data.SaveChanges();
return this.Ok(string.Format("user: {0} is not admin now", user.UserName));
}
示例2: Demote
public ActionResult Demote(string id)
{
var currentUser = this.Data.Users.All().First(u => u.UserName == User.Identity.Name);
var isSameUser = id == currentUser.Id;
if (!isSameUser)
{
var store = new UserStore<User>(this.Data.Context);
var manager = new UserManager<User>(store);
manager.RemoveFromRoleAsync(id, GlobalConstants.AdminRole);
this.Notify(GlobalConstants.DemoteUserSuccess, NotificationType.success);
}
else
{
this.Notify(GlobalConstants.DemoteUserFail, NotificationType.error);
}
return this.RedirectToAction("Manage");
}
示例3: UpdateRuoloUtente
public async Task<IHttpActionResult> UpdateRuoloUtente(UpdateRuoloUtenteModel Model)
{
if (Model == null || !ModelState.IsValid)
return BadRequest(ModelState);
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
var utente = await userManager.FindByNameAsync(Model.Username);
if (utente == null)
return NotFound();
if (Model.NuovoStato)
{
await userManager.AddToRoleAsync(utente.Id, Model.Ruolo);
}
else
{
await userManager.RemoveFromRoleAsync(utente.Id, Model.Ruolo);
}
}
return Ok();
}
示例4: DeleteConfirmed
public async Task<ActionResult> DeleteConfirmed(string id) {
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
if (ModelState.IsValid) {
if (id == null) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await userManager.FindByIdAsync(id);
// ev. 3parts inloggningar
var logins = user.Logins;
foreach (var login in logins.ToList()) {
await userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
var rolesForUser = await userManager.GetRolesAsync(id);
if (rolesForUser.Count() > 0) {
foreach (var item in rolesForUser.ToList()) {
// item should be the name of the role
var result = await userManager.RemoveFromRoleAsync(user.Id, item);
}
}
if (user.Documents.Count() > 0) {
foreach (var doc in user.Documents.ToList()) {
db.Documents.Remove(doc);
}
}
await userManager.DeleteAsync(user);
return RedirectToAction("Index");
}
else {
return View();
}
}
示例5: RoleCheckBox_CheckChanged
protected void RoleCheckBox_CheckChanged(object sender, EventArgs e)
{
// Reference the CheckBox that raised this event
CheckBox RoleCheckBox = sender as CheckBox;
// Get the currently selected user and role
string selectedUserName = UserList.SelectedValue;
var um = new UserManager();
var user = um.FindByName(selectedUserName);
string roleName = RoleCheckBox.Text;
// Determine if we need to add or remove the user from this role
if (RoleCheckBox.Checked)
{
// Add the user to the role
um.AddToRoleAsync(user.Id, roleName);
// Display a status message
ActionStatus.Text = string.Format("User {0} was added to role {1}.", selectedUserName, roleName);
}
else
{
// Remove the user from the role
um.RemoveFromRoleAsync(user.Id, roleName);
// Display a status message
ActionStatus.Text = string.Format("User {0} was removed from role {1}.", selectedUserName, roleName);
}
// Refresh the "by role" interface
DisplayUsersBelongingToRole();
}
示例6: EditbuildingUserPermission
/// <summary>
/// remove all current permission for the given user and insert new given permissions.
/// </summary>
/// <param name="newRoles">Accepts a list of RolesName type string</param>
/// <param name="UserID">Accepts a 128 chr userID type string</param>
/// <returns></returns>
public async Task<string> EditbuildingUserPermission(List<string> newRoles, string UserID)
{
var result = "";
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
//get all roles for the current user
List<string> AllRoles = UserManager.GetRoles(UserID).ToList();
//delete all the roles
foreach (var item in AllRoles)
{
await UserManager.RemoveFromRoleAsync(UserID, item);
}
//add new roles
foreach (var item in newRoles)
{
if (RoleManager.RoleExists(item))
{
await UserManager.AddToRoleAsync(UserID, item);
}
}
return result;
}