ASP.NET Web Pages – Remove User from Role

[ This is a 5 part tutorial; previous tutorial, Add User to Role ]

Now we need to add another file which will allow us to remove users from roles. Create a new file in the private folder and name it RemoveUserFromRole. Then copy this:



    var db = Database.Open("StarterSite");
    var SQLUSER = "SELECT Email FROM UserProfile";
    var users = db.Query(SQLUSER);
    var roles = Roles.GetAllRoles();//GET ALL ROLES

    var username = Request["username"];
    var role = Request["role"];
    var msg = string.Empty;

    if (IsPost){        

        if (Roles.IsUserInRole(username, role)){//MAKE SURE USER IS IN ROLE
        Roles.RemoveUserFromRole(username, role);//REMOVE THE USER FROM THE ROLE
        msg = username + " Has been removed from the role " + role;
            msg = username + " is not in the role " + role;


This is similar to the previous page however this time we simply remove the user from the role.


<form method="post">
    <p style="color:#a33f3f">@msg</p>

<legend>Remove User From Role</legend>

<div class="elements">
    <label for="user">User:</label>
    <select name="username" id="user">
    @foreach (var person in users){

    <div class="elements">
    <label for="role">Role:</label>
    <select name="role" id="role">
    @foreach (var uRole in roles){
    <input type="submit" class="submitform"/>


[ continue, Delete Role ]