简体   繁体   English

在其他页面中使用其他用户登录?

[英]Login using different user in a different page?

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

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class OnlineAppSyss : System.Web.UI.Page
    {
        private MSConnector connector = new MSConnector();
        //string queries for each DataSet
        string query = null;
        string teacherquery = null;
        string subjectquery = null;
        string schoolfeequery = null;
        string accountdetailsquery = null;
        int rowcounter = 0;
        int teachercounter = 0;

        //DataSet and DataTable initialization
        private DataSet studentData;
        private DataSet subjectData;
        private DataSet schoolfeeData;
        private DataSet teacherData;
        private DataSet accountdetailsData;
        private DataTable subjectTable;
        private DataTable schoolfeeTable;
        private DataTable accountdetailsTable;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dropdownrole.SelectedItem.Value == "Admin")
            {
                Admin();
            }
            else if (dropdownrole.SelectedItem.Value == "Student")
            {
                Student();
            }
            else if (dropdownrole.SelectedItem.Value == "Teacher")
            {
                Teacher();
            }
        }
        public void Admin()
        {
            //String decleration
            string adminusername = (this.UserName.Value);
            string adminpass = (this.Password.Value);

            try
            {
                if (adminusername == "admin" && adminpass == "cmpe1234")
                {
                    Session["adminlogin"] = adminusername;
                    Response.Redirect("AdministratorPage.aspx");
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }
        }
        public void Student()
        {
            //Connection String
            connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

            //String decleration
            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            //query database from sql server management studio for student
            query = "select studentid,password,firstname,lastname,course,year from student";

            //execute query for student
            studentData = connector.ExecuteQuery(query);


            try
            {
                for (; ; )
                {
                    //string decleration and getting each rows of the Student database
                    string userid = studentData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
                    string password = studentData.Tables[0].Rows[rowcounter]["Password"].ToString();
                    string firstname = studentData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
                    string lastname = studentData.Tables[0].Rows[rowcounter]["LastName"].ToString();
                    string course = studentData.Tables[0].Rows[rowcounter]["Course"].ToString();
                    string year = studentData.Tables[0].Rows[rowcounter]["Year"].ToString();


                    //For Student Condition
                    if (username == userid && pass == password)
                    {
                        //For Student Data Sessions
                        Session["login"] = userid;
                        Session["firstname"] = firstname;
                        Session["lastname"] = lastname;
                        Session["course"] = course;
                        Session["year"] = year;

                        //For Account Details Data
                        accountdetailsquery = "select StudentID,FirstName,MiddleName,LastName,Age,Province,City,Course,Year,College,Department,ContactNumber,Email from student where studentid = " + username + "";

                        //query database from sql server management studio for student as accountDetails Information
                        accountdetailsData = connector.ExecuteQuery(accountdetailsquery);

                        accountdetailsTable = accountdetailsData.Tables[0];
                        Session["AccountDetails"] = accountdetailsTable;

                        //For SchoolFee Data
                        //query database from sql server management studio for schoolfee
                        schoolfeequery = "select DatePaid,AmountPaid,CurrentBalance,TotalBalance,Semester from schoolfee where studentid = " + username + "";

                        //execute query for schoolfee
                        schoolfeeData = connector.ExecuteQuery(schoolfeequery);

                        //get all data rows for SchoolFee and store it into DataTable
                        schoolfeeTable = schoolfeeData.Tables[0];
                        Session["SchoolFee"] = schoolfeeTable;

                        //For Subject Data
                        //query database from sql server management studio for subject
                        subjectquery = "select CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room from subject where studentid = " + username + "";

                        //execute query for subject
                        subjectData = connector.ExecuteQuery(subjectquery);

                        //get all data rows for Subject and store it into DataTable 
                        subjectTable = subjectData.Tables[0];
                        Session["Subjects"] = subjectTable;

                        //Redirect the page to Student Page after the user successfully logs in.
                        Response.Redirect("StudentPage.aspx", true);

                        break;
                    }
                    else
                    {
                        rowcounter++;
                    }
                }

            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }

        }
        public void Teacher()
        {
            //Connection String
            connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

            //String decleration
            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            //query database from sql server management studio for student
            teacherquery = "select teacherid,password,firstname,lastname,department,position from teacher";

            //execute query for student
            teacherData = connector.ExecuteQuery(teacherquery);

            try
            {
                for (; ; )
                {
                    //string decleration and getting each rows of the Teacher database
                    string teacheruserid = teacherData.Tables[0].Rows[rowcounter]["TeacherID"].ToString();
                    string teacherpassword = teacherData.Tables[0].Rows[rowcounter]["Password"].ToString();
                    string teacherfirstname = teacherData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
                    string teacherlastname = teacherData.Tables[0].Rows[rowcounter]["LastName"].ToString();
                    string teacherdepartment = teacherData.Tables[0].Rows[rowcounter]["Department"].ToString();
                    string teacherposition = teacherData.Tables[0].Rows[rowcounter]["Position"].ToString();

                    //For Teacher Condition
                    if (username == teacheruserid && pass == teacherpassword)
                    {
                        Session["teacherlogin"] = teacheruserid;
                        Session["teacherfirstname"] = teacherfirstname;
                        Session["teacherlastname"] = teacherlastname;
                        Session["department"] = teacherdepartment;
                        Session["position"] = teacherposition;

                        //Redirect the page to Student Page after the user successfully logs in.
                        Response.Redirect("TeacherPage.aspx", true);
                        break;
                    }
                    else
                    {
                        rowcounter++;
                    }
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again (teacher)');</script>");
            }
        }
    }
}

My problem is I can't access the teacher page in this code, it will only access the student page. 我的问题是我无法访问此代码中的教师页面,它将仅访问学生页面。 What Condition should I use in my system to avoid redundancy? 我应该在系统中使用什么条件来避免冗余?

This is my aspx Code for dropdownlist: 这是我的aspx下拉列表代码:

<asp:DropDownList runat="server" id="dropdownrole">
    <asp:ListItem Text="Admin">Admin</asp:ListItem>
    <asp:ListItem Text="Student">Student</asp:ListItem>
    <asp:ListItem Text="Teacher">Teacher</asp:ListItem>
</asp:DropDownList>

and the login button: 和登录按钮:

 protected void Button1_Click(object sender, EventArgs e)
        {
            if (dropdownrole.SelectedItem.Value == "Admin")
            {
                Admin();
            }
            else if (dropdownrole.SelectedItem.Value == "Student")
            {
                Student();
            }
            else if (dropdownrole.SelectedItem.Value == "Teacher")
            {
                Teacher();
            }
        }

I want to have a condition that If a username and password input detects either of the 2 users, it will redirect to their specific webpage. 我希望有一个条件,如果用户名和密码输入检测到2个用户中的任何一个,它将重定向到他们的特定网页。 Please Help. 请帮忙。

You didn't select teacher password in your teacherquery therefor the teacherpassword is always null that why the condition password == teacherpassword is always false. 您没有在您选择的老师密码teacherquery为此该teacherpassword总是空,之所以条件password == teacherpassword永远是假的。

Like DPac pointed out, you don't need to query all of rows just to verify the username & password, just select username & password only first, if it is correct then select all of rows you want and assign them to sessions, then redirect. 就像DPac指出的那样,您无需查询所有行就仅需验证用户名和密码,只需仅选择用户名和密码,如果正确,则选择所需的所有行并将其分配给会话,然后重定向。

About the problem if student's username is as same as teacher's username, there will be whole lot of troubles, to avoid it (without doing whole lot of thing) is to add 1 more int row in both tables named userRole (ex: 1 = admin, 2 = teacher, 3 = student etc.) and allow it to be null. 关于该问题,如果学生的用户名与老师的用户名相同,将会有很多麻烦,要避免该问题(不做很多事情)是在两个名为userRole表中再添加1个int行(例如:1 = admin ,2 =老师,3 =学生等),并将其设置为空。 After that go in each table and update that row to proper value (for example in student table: UPDATE student SET userRole = 3 ). 之后,进入每个表并将该行更新为适当的值(例如,在学生表中: UPDATE student SET userRole = 3 )。 After update all of row then go to Design and set the userRole to not allow null Now the username + userRole combination (should have been your primary key) will make your life much easier. 更新所有行之后,请转到“设计”,并将userRole设置为不允许为null现在,用户名+ userRole组合(应该是您的主键)将使您的工作变得更加轻松。

Although i would prefer @ronaldinho's way of giving a RoleId column to both tables.Here is what you can do without disturbing the database. 尽管我更喜欢@ronaldinho为两个表提供RoleId列的方法。这是您可以做的而又不会打扰数据库。

Just add a dropdownlist with items "Teacher" and "Student" on your login page.So when some one tries to log in they have to select their role and then enter. 只需在登录页面上添加一个包含“教师”和“学生”项目的下拉列表。因此,当有人尝试登录时,他们必须选择自己的角色,然后输入。 This way you can direct the code as 这样,您可以将代码定向为

   protected void Button1_Click(object sender, EventArgs e)
    {
        if (dropdownrole.SelectedItem.Text == "Admin")
        {
            Admin();
        }
        else if (dropdownrole.SelectedItem.Text == "Student")
        {
            Student();
        }
        else if (dropdownrole.SelectedItem.Text == "Teacher")
        {
            Teacher();
        }
    }

See if it is possible for you 看看有没有可能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM