Redirect Unauthorized Page Access in MVC to Custom View

后端 未结 5 1716
温柔的废话
温柔的废话 2020-12-23 16:36

I have an MVC website in which access is based on various Roles. Once a user logs into the system they can see navigation to the pages for which they are authorized. However

5条回答
  •  独厮守ぢ
    2020-12-23 17:04

    To redirect-unauthorized-page-access-Asp.net C# -to-custom-view

    1. on page load add this in login.aspx.cs
         protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    
                    txtUsername.Focus();
                    if (!IsPostBack)
                    {
                        if (Request.IsAuthenticated && !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                        {
                            Response.Redirect("UnauthorizedAccess.aspx", false);
                            return;
                        }                   
                        Session.Clear();
                        Session.Abandon();
    
                    }                
                }
                catch (Exception ex)
                {
                    this.errLogin.Text = ex.Message;                
                }
            }
    

    2)create UnauthorizedAccess.aspx

        

    Unauthorized

    Sorry, you are not authorized to access this page

    Back to Home

提交回复
热议问题