Click here to Skip to main content
14,579,712 members
Rate this:
Please Sign up or sign in to vote.
See more:
I created web application, in that
1- i want hide some master page contents(ex- links to other pages(Menu), buttons)

2- i want to restrict the some page access for logged in users and non logged in users.

i have 5 pages viz homepage,register,transactions,view_transactions,dashboard

i shld restrict accessing the pages transactions,view_transactions,dashboard to non logged in users.

how to achieve this in c# asp.net.


My master page code:

 <pre><%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Expense.master.cs" Inherits="Expense_System.Expense" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="App_Themes/Theme1/MasterPageStyle.css" rel="stylesheet" />

    <title> Expense System</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
        
             

    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">

    <div>
      
            <asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" Orientation="Horizontal" RenderingMode="Table" StaticSubMenuIndent="10px" style="font-weight: 700; color: #FFFFFF; background-color: #99CCFF; font-size: xx-large; text-align: center;" Width="99%" Height="67px">
                <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
                <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
                <DynamicMenuStyle BackColor="#FFFBD6" />
                <DynamicSelectedStyle BackColor="#FFCC66" />
                <Items>
                    <asp:MenuItem NavigateUrl="~/HomePage.aspx" Text="Home Page" Value="Home Page"></asp:MenuItem>
                    <asp:MenuItem NavigateUrl="~/Register.aspx" Text="Register" Value="Register"></asp:MenuItem>
                    <asp:MenuItem NavigateUrl="~/Loginpage.aspx" Text="Login" Value="Login"></asp:MenuItem>
                </Items>
                <StaticHoverStyle BackColor="#990000" ForeColor="White" />
                <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
                <StaticSelectedStyle BackColor="#FFCC66" />
            </asp:Menu>
  
    

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  
          
  
          
  
        </asp:ContentPlaceHolder>
          

    </div>
    </form>
</body>
</html>



<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace Expense_System
{
    public partial class Loginpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnlogin_Click(object sender, EventArgs e)
        {

            String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand("SELECT * FROM User_details WHERE [Login Id]=@loginid AND [Password]=@password",con );
            cmd.Parameters.Add("@loginid", SqlDbType.VarChar).Value = loginid.Text;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pwd.Text;

            //cmd.CommandType = CommandType.StoredProcedure;

            //con.Open();

            //cmd.CommandText = "SPchkloginuser";

            //cmd.Parameters.Add("@loginid", SqlDbType.VarChar).Value = loginid.Text;
            //cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pwd.Text;
            //cmd.Connection = con;


            //SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText,con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count > 0)
            {

                Response.Redirect("Transactions.aspx");
               
            }

            else
            {
                
                Response.Write("<script>alert('Invalid credentials')</script> ");
               
            }
        }
    }
}


What I have tried:

I am not getting logic, i'm new to coding and language also
Posted
Updated 1-Aug-18 6:14am
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you are new to ASP.NET and wanted to implement page access security, I'd suggest you to start looking at: Walkthrough: Managing Web Site Users with Roles[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

If you do not want to get into something generic, you can access controls of the master page using Master.FindControl from your content page...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100