Click here to Skip to main content
15,867,704 members

How to check role based authentication on button click event

amitesh1989 asked:

Open original thread
I have a login form in which user have to enter credential i.e. username and password.In my code i check that user whether it is an authorized user or not now i also want to check what is the access level of that user according to its role

here is my login page code:

C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    String queryread = @"Select * from Login where UserName = '" + tbUserName.Text.ToLower() + "' and Password='"+ tbPassword.Text.ToLower() +"'";
    SqlConnection con = new SqlConnection();
    SqlDataReader read;
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["BartConnectionString"].ConnectionString;
    SqlCommand readdata = new SqlCommand(queryread, con);
    try
    {
        con.Open();
        read = readdata.ExecuteReader();
        Boolean flag = false;
        while (read.Read())
        {
            String UserName = read["UserName"].ToString().ToLower();
            String password = read["Password"].ToString();
            if (tbUserName.Text.ToLower() == UserName)
            {
                if (tbPassword.Text.Trim() == password)
                {
                        Session["UserID"] = UserName.ToString();
                        flag = true;   
                }
            }
        }
        if (flag)
        {

            Response.Redirect("~/Supervisor/Form.aspx",false);
        }
        else
        {
            Response.Redirect("~/Error.aspx",false);
        }
    }
    catch (Exception ex2)
        {
            Response.Write("Error");
        }
    finally
        {
        if (con.State == System.Data.ConnectionState.Open)
            con.Close();
        }
    }


in the upper code i check authorized user from database and then redirect user accordingly to particular folder and error page. now i want to check if that user is admin it will redirect to admin page in admin folder, if it is manager it will redirect to manager page in manager folder and so on.

for this i need to know.

Database schema for it
What is the code used in web.config file to provide access according to roles.

and what if i need to make some changes then let me know guys.

Thanks in Advance
Tags: C#, ASP.NET4.0, Database Development, Authentication

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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