Click here to Skip to main content
15,901,921 members
Please Sign up or sign in to vote.
1.00/5 (1 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:

C#
 <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>



C#
<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
Comments
F-ES Sitecore 30-Jul-18 4:42am    
This should be done in the master page. It it's page_load just have code like

if (userNotLoggedOn)
{
targetControl.Visible = false;
}
Richard Deeming 3-Aug-18 15:06pm    
NEVER store passwords in plain text!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems built-in - for example, ASP.NET Identity[^]

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[^]
 
Share this answer
 
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...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900