Wednesday, 16 April 2014

In C# Code Behind Validate Valid user or not using basepage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for BasePage
/// </summary>

public class BasePage : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        string cTheFile = HttpContext.Current.Request.Path;

        // here select what part of that string you won to check out
        if (!GetAuthenticatedRolesByModuleName(cTheFile))
        {
            // some how avoid the crash if call the same page again
            if (!cTheFile.EndsWith("Default.aspx"))
            {
                Response.Redirect("Default.aspx", true);
                return;
            }
        }

        // continue with the rest of the page
        base.OnPreInit(e);
    }

    private bool GetAuthenticatedRolesByModuleName(string cTheFile)
    {
        if (Session["Name"]!=null)
        {
            return true;
        }
        else
        {

            return false;
        }
    }
}

No comments:

Post a Comment