Make a user view a different navbar items than admin when logging in on the same page?

Viewed 67

Note: I'm using ASP.NET Core Web Application (.NET Framework) and C# and SQL Server I need to specify each admin and user description in database and to show it on MainPage every time I give a new description or remove it.

This is my Login Page:

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

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

        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=Users;Integrated Security=True");
            con.Open();
            string query = "SELECT username, password FROM UsersRole WHERE username = '" + TxTUser.Text + "' and password = '" + TxTPass.Text + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                LabelMSG.Text = "You have Successfully Logged in!";
                Response.Redirect("MainPage.aspx");
            }
            else
            {
                LabelMSG.Text = "Please Check your username or password!";
            }
            con.Close();
        }
    }
}

And this is my MainPage:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="WebApplication2.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <nav>
        <ul>
            <li id="item1"><a href="#">Accounting</a></li>
            <li id="item2"><a href="#">IT</a></li>
            <li id="item3"><a href="#">Policies</a></li>
            <li id="item4"><a href="#">Sales</a></li>
            <li id="item5"><a href="#">Marketing</a></li>
            <li id="item6"><a href="#">Equipment</a></li>
            <li id="item7"><a href="#">Transportation</a></li>
            <li id="item8"><a href="#">Printer</a></li>
            <li id="item9"><a href="#">Cashier</a></li>
            <li id="item10"><a href="#">HR</a></li>
        </ul>
    </nav>
</body>
</html>
1 Answers

There is not a quick answere to this. Here a a few steps I would use to achieve this: Microsoft Identity Framework

Create a 'MyAppState.cs' via Dependency Injection in the Startup.cs to use in everypage later.

  1. It looks like the password is stored as plain txt. That is bad. Look into Bcrypt to hash your password and save that hash to DB.
  2. Look into Dependecy Injection to create a Class that you can then use globaly via AddScoped Service in the startup.cs.

This 'AddScoped Service' will create a Object that you can inject to the webpages and get the logininformation this way.

  1. After DI in starup use RazorPageSyntax in the webpages to check if user is authenticated

pseudocode for mainpages: @inject MyAppState myAppState //injected object with the Auth-Propertys set on the login page

@if(myAppState.UserIsAuthAsAdmin) { ...show admin menue } else{ ... show differened menue }

You should work it out if you look into DependencyInjection and RazorpageSyntax. The better way but with higher learning curve would be Identity Framwork.

Related