Search code examples
asp.netmembership

How to allow only specific users to specific pages when using query parameters like ...?id=3


I have a page that shows some data depending on the id param in request.

The user must be logged in

How should I stop the user from accessing certain ids that belong to other users. Is redirect the best choice?


Solution

  • You can check on the server-side before rendering the page, then redirect...etc:

    void Page_Load(object sender, EventArgs e)
    {    
        string bla = Request["key"] != null ? Request["key"] : "";
        if(String.IsNullOrEmpty(bla))
        if (bla=="yourvaluetocheck" && !User.IsInRole("theroletouse"))
        {
            Response.Redirect("~/permissiondenied.aspx");
        }
    }
    

    This a very simple example, I would put this type of logic in a central place in my application for re-use.