Redirecting user to NotAuthorized page when tried accessing a restricted resource
When we use forms authentication with roles, the logged in user will be again forwarded to login page when he tries accessing a resource which his role is restricted.
This can be prevented and the user can be redirected to NotAuthorized page by adding the following code snippet in the Login Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated && Request.QueryString["ReturnUrl"] != null)
{
Response.Redirect("NotAuthorized.aspx");
}
}
When the above included in the Login.aspx page load event, the user will be automatically redirected to Not Authorized Page when he tries accessing a restricted resource!
|