title: How to assign role and web page permission to a user using aspx?
body: <p>In my Web App I have the ability to create a user and role.  But what I would like to do now is to assign a page to that user in that role.</p>
  
  <p>So for example:</p>
  
  <p>User1 is in role "Clients" and can only have access to WebForm1.aspx</p>
  
  <p>As per below:</p>
  
  <p><a href="https://i.stack.imgur.com/GiBX4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GiBX4.png" alt="enter image description here"></a></p>
  
  <p>I guess I need to save the webpages locations in my database and link the user and role to that page and then check in the page load event what user is logged in to redirect them to that page only?</p>
  
  <p>The problem is I am not sure on how to achieve this.  </p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Updated Code:</p>
  
  <pre><code>public partial class Login : System.Web.UI.Page
  {
  
      protected void btnLoginButton_Click(object sender, EventArgs e)
      {
  
          if (Membership.ValidateUser(UserName.Text, Password.Text))
          {
              FormsAuthentication.SetAuthCookie(UserName.Text, true);
              Session["UserName"] = this.UserName.Text.Trim();
              CheckPermission();
  
              FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);
  
  
          }
  
          // If we reach here, the user's credentials were invalid
          InvalidCredentialsMessage.Visible = true;
      }
  
      public string GetUserPage(string userId)
      {
          SqlConnection conn = new SqlConnection(@"Data Source=DEV\SQLEXPRESS;Initial Catalog=SecurityDB;Integrated Security=True");
          SqlCommand cmd = new SqlCommand("SELECT * FROM aspnet_Permissions WHERE UserId = @userId", conn);
          cmd.Parameters.AddWithValue("@userId", userId);
          cmd.CommandType = CommandType.Text;
  
          SqlDataAdapter da = new SqlDataAdapter();
          da.SelectCommand = cmd;
          DataTable dt = new DataTable();
          da.Fill(dt);
          foreach (DataRow row in dt.Rows)
          {
              if (row["FilePath"] != null)
              {
                  return row["FilePath"].ToString();
              }
          }
          return null;
      }
  
      public void CheckPermission()
      {
          var userPage = GetUserPage(User.Identity.Name);
          if (string.IsNullOrWhiteSpace(userPage))
          {
              Response.Redirect("/NotAllowed");
          }
          if (Request.RawUrl != userPage)
          {
              Response.Redirect(userPage);
          }
      }
  }
  </code></pre>
  
  <p>In my data base table <code>aspnet_Permissions</code> I have the following:</p>
  
  <pre><code>-----------------------------------------------------------------------
  | ID    |  UserId  | FilePath                                         |
  -----------------------------------------------------------------------
  | 1     | 123Admin | C:\Users\Development\Web\Clients\123.aspx        |
  -----------------------------------------------------------------------
  </code></pre>
  
  <p>When I try and log in now with user <code>123Admin</code> I get redirected to the "Not Allowed Page"</p>
  
  <p>I understand the concept now better but not sure why it isn't working now?</p>
  
  <p>Thanks</p>