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:

C#

@{    

    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;
        }else{
            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.

HTML

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

<fieldset>
<legend>Remove User From Role</legend>

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

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

</fieldset>
</form>

[ continue, Delete Role ]